有一未知控件,判定此控件具有Items属性后,如何将Items属性的内容化为字符形式?(200分)

  • 主题发起人 wuyaobin
  • 开始时间
W

wuyaobin

Unregistered / Unconfirmed
GUEST, unregistred user!
如Items有三项:
one
two
three
怎样化为字符串'one,two,three'?(注:事前不知道是什么控件)
要求具有Items属性的各类控件都通用,不能分别判断(如TListBox,TComboBox,TGroupBox;
TCheckListBox分别判断),如何实现?
 
通用的恐怕不行,TControl没有Items这个属性,不能用(Sender as TControl).items来
赋值,可能还是要对含有Items的控件作一个个判断。
 
如果是Published属性,可以用RTTI来做的。
uses typinfo;

var
MyObj:TObject;

function GetItemProp(Obj:TObject;PropName:String):TObject;
begin
Result:=nil;
try
Result:=GetObjectProp(Obj,propName);
except
end;
end;
 
to:djdsz
关键是如何把Items属性的字符串组赋值给String变量.
 
如:下列代码可得到具有Caption的值,怎样得到Items(TStrings)的值
并转为String呢?
var
P: PPropInfo;Str:string;fc: TComponent;
for i := 0 to ComponentCount- 1 do begin
fc := Components;
P := GetPropInfo(fc, 'Caption');
if (P <> nil) and (Trim(GetStrProp(fc, P.Name)) <> '') then
str:=GetStrProp(fc, P.Name));
end;
 
GetObjProp可以取得对象属性, 然后as TStrings就行了.
TStrings.Text可以得到全部string,Strings[]可以得到每一行
 
var
s:string;

s:=items.commaText;//[:)][:)]
以下可用来测试:

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
showmessage(combobox1.items.commatext);
for i:=0 to combobox1.items.Count-1 do
showmessage(combobox1.items);

end;
 
这是我在实际中用的,希望对你有用(你必须用D5或以上的版本)
PropInfo := GetPropInfo(m, 'items');
if PropInfo <> nil then
begin
tempObj := GetObjectProp(m, PropInfo);
if tempObj <> nil then
if tempObj.InheritsFrom(TStrings) then
acap := TStrings(tempObj).Text;
end;
 
接受答案了.
 
顶部