全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:5402
推到 Plurk!
推到 Facebook!

How to change properties of objects just by name?

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-02-11 14:14:40 IP:61.218.xxx.xxx 未訂閱
How to change properties of objects just by name?    資料來源:www.delphi3000.com    How can i access properties of classes that are not implemented via  the uses-clause, just knowing their names ( by string)?     You have to use the TypInfo unit.  Simple properties, like strings and integer, can be accessed in the  following manner: 
------------------------------------------------------------- 
Uses TypInfo;     procedure AlterProp(AName, APropName, AValue:string); 
var  i : integer; 
     C : TComponent; 
begin 
    // Run through all Components to find the right Component 
    for i:=0 to Form1.Componentcount-1 do 
    begin 
        C := Form1.Components[i]; 
        if (C.Name = AName) then 
        begin 
            SetPropValue(C,APropName, AValue); 
       end; 
    end; 
end; 
------------------------------------------------------------- 
Now there are also some Properties like Font. How do i reach those sub-properties? Here is a solution to that:
------------------------------------------------------------- 
Uses TypInfo;     procedure AlterFontColor(AName:string; AColor:TColor); 
var  i : integer; 
     C : TComponent; 
     AObj : TObject; 
begin 
    // Run through all Components to find the right Component 
    for i:=0 to Form1.Componentcount-1 do 
    begin 
        C := Form1.Components[i]; 
        if (C.Name = AName) then 
        begin 
            AObj := GetObjectProp(C,'Font'); 
            SetPropValue(AObj,'Color',AColor); 
        end; 
    end; 
end; 
------------------------------------------------------------- 
And finally you have many indexed properties like TStrings or stuff. Now how do i reach those indexed properties? All indexed properties are stored in TCollection-Objects. So you have to Typecast them like in the following function:
------------------------------------------------------------- 
Uses TypInfo;     procedure AlterIndexObject(ACompName, APropName:string; ACaption: string); 
var  i : integer; 
     C : TComponent; 
     ACollectionItem, AObj : TObject; 
begin 
    // Run through all Components to find the right Component 
    for i:=0 to Form1.Componentcount-1 do 
    begin 
        C := Form1.Components[i]; 
        if (C.Name = ACompName) then 
        begin 
            AObj:= GetObjectProp(C,APropName); 
            ACollectionItem := TCollection(AObj).Items[0]; 
            SetPropValue(ACollectionItem, 'Caption', ACaption); 
        end; 
    end; 
end; 
------------------------------------------------------------- 
There are many possibilities for this functions to use in your projects. Mainly there are usefull for language-changings or skin-components. The possibilities are endless. I hope my article was somewhat usefull for you. I like to share knowledge with other developers. keep on coding :-) Jürgen Sommer 聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
系統時間:2024-11-21 18:24:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!