線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1215
推到 Plurk!
推到 Facebook!

找所有包含有Items(TStrings) 這屬性的元件, 且對這些 item[idx] 做存取

尚未結案
kevin@ROC
一般會員


發表:2
回覆:0
積分:0
註冊:2004-08-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-09-16 21:03:25 IP:218.172.xxx.xxx 未訂閱
我想要在一個 Form 中, 將所有元件中, 有 Caption 這屬性的元件找出來, 並且給予設定一特定值... 以下是 ha0009 所提的解答...很感謝! type TAccessControl = class (TControl) end; procedure SetAllCaption (Form : TForm; Const Caption : String); var idx : integer; PropInfo: PPropInfo; begin for idx := 0 to Form.ControlCount - 1 do begin TAccessControl(Form.Controls [idx]).Caption := Caption; // 註一 PropInfo := GetPropInfo(Form.Controls [idx], 'Caption'); // 註二 if PropInfo <> nil then begin SetPropValue (Form.Controls [idx], 'Caption', Caption); end; end; end; 但是, 若如果我想找所有包含有 Items (TStrings) 這屬性的元件, 而且對這些 item[idx] 做讀取且指定值給它的話...那不知道可不可行呢?
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-09-16 23:04:51 IP:218.32.xxx.xxx 未訂閱
kevin@ROC 你好:    Form1上, 放一個 ComboBox, 試試看:
function GetTStringsProp(Sender: TObject):TStrings;
var P : PPropList;
    i,R : integer;
begin
  Result := nil;
  R := GetPropList(Sender,P);
  for i := 0 to R-1 do
    if (GetEnumName(TypeInfo(TTypeKind),Ord(P^[i].PropType^.Kind))='tkClass')and
       (CompareText(P^[i].Name,'Items')=0) and // Property Name is 'items'
       (CompareText(P^[i].PropType^.Name,'TStrings')=0) // Class Name is 'TStrings' 
    then Result := TStrings(GetObjectProp(Sender,P^[i].Name));
end;    procedure TForm1.Button1Click(Sender: TObject);
var Temp: TStrings;
begin
  Temp := GetTStringsProp(combobox1);
  if Temp <> nil then Temp.Add('Add One Item');
end;
發表人 - Mickey 於 2004/09/16 23:09:42
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-09-17 16:23:57 IP:61.222.xxx.xxx 未訂閱
uses typinfo; function PropertyIsTStrings(PropInfo : PPropInfo):Boolean; var Pinfo:TTypeInfo; LowePName:String; begin Pinfo :=GetPropertyTypeInfo(PropInfo); LowePName :=LowerCase(PInfo.Name); Result :=(PInfo.Kind=tkClass) and ((LowePName='tstrings') or (LowePName='tstringlist')); end; procedure TForm1.Button1Click(Sender: TObject); var I :integer; PropInfo : PPropInfo ; AComponent : TComponent; Items:TStringList; PS :Pointer; begin Items :=TStringList.Create; Try For I:= 0 to ComponentCount-1 do begin AComponent := Components[I]; PropInfo := GetPropInfo(AComponent.classInfo,'ITEMS'); if PropertyIsTStrings(PropInfo) then begin PS :=Pointer(GetOrdProp(AComponent,PropInfo)); Items.Assign(Tstrings(PS)); { 修改該 Items} if Items.Count>0 then Items[0]:='XXXXXX'; SetOrdProp(AComponent,PropInfo,LongInt(Pointer(Items))); end; end; finally Items.Clear; Items.Free; end; end;
系統時間:2024-05-06 2:42:59
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!