我想编程序实现以下操作,谁知道怎么做   For i=0 to Form.Componets.count-1 do 
   begin
      if  Exists  Componets[i].Items:TStrings   then 
         .......
   end;是否可以通过程序实现呢?

解决方案 »

  1.   

    uses TypInfo;
    .......
    function getProp():boolean;
    begin
        try
          GetPropValue(button1,'Caption');
          result:=true;
        except
          result:=false;
        end;
    end;
      

  2.   

    For i=0 to Form.Componets.count-1 do 
       begin
          if Componets[i] is Tclassa  then //TClasssA为具有items属性的基类
             TClassa(Componets[i]).Items...///
             .......
       end;
      

  3.   

    请问 linzhisong: Variant 和 TStrings 之间怎么互相转换
    请问 firetoucher: TClasssA为具有items属性的基类
    这个基类怎么表示?
      

  4.   

    Sorry,第一个问题我找到答案了。但是我用了一下setpropvalue,不成功啊(我get完还要set回去的)请看:var i:integer;
        mylist:TStrings;
        Newvalue:Variant;
    begin
       mylist:=TSTringList.Create;
       mylist.Clear;
       mylist.add('1111111111111111');
       mylist.add('2222222222222222');
       for i:=0 to ComponentCount-1 do
       begin
         Try
            Newvalue:=VarArrayFromStrings(mylist);
            Setpropvalue(Components[i],'Items',Newvalue);
          Except      end;
       end;
       mylist.free;
    end;
      

  5.   

    可能理解错了,应该这样吧
        if TClassa(Componets[i]).Items is TStrings then
            caption:=datetimetostr(now);
      

  6.   

    另,我试过 getpropvalue是可以的,Form上有一个listbox.可是Setpropvalue就报错 invalid property type: TStrings
      

  7.   

    linzhisong,我就是不知道 TClassa这个类怎么去Type 要知道了不就简单了。
      

  8.   

    这个只能自己去判断
    delphi里面方法:
    function GetPropValue(Instance: TObject; const PropName: string;
      PreferStrings: Boolean): Variant;
    var
      PropInfo: PPropInfo;
    begin
      // assume failure
      Result := Null;  // get the prop info
      PropInfo := GetPropInfo(Instance, PropName);
      if PropInfo = nil then
    PropertyNotFound(PropName)
      else
      begin
    // return the right type
    case PropInfo^.PropType^^.Kind of
      tkInteger, tkChar, tkWChar, tkClass:
    Result := GetOrdProp(Instance, PropInfo);
      tkEnumeration:
    if PreferStrings then
      Result := GetEnumProp(Instance, PropInfo)
    else if GetTypeData(PropInfo^.PropType^)^.BaseType^ = TypeInfo(Boolean) then
      Result := Boolean(GetOrdProp(Instance, PropInfo))
    else
      Result := GetOrdProp(Instance, PropInfo);
      tkSet:
    if PreferStrings then
      Result := GetSetProp(Instance, PropInfo)
    else
      Result := GetOrdProp(Instance, PropInfo);
      tkFloat:
    Result := GetFloatProp(Instance, PropInfo);
      tkMethod:
    Result := PropInfo^.PropType^.Name;
      tkString, tkLString:
    Result := GetStrProp(Instance, PropInfo);
      tkWString:
    Result := GetWideStrProp(Instance, PropInfo);
      tkVariant:
    Result := GetVariantProp(Instance, PropInfo);
      tkInt64:
    Result := GetInt64Prop(Instance, PropInfo);
      tkDynArray:
    DynArrayToVariant(Result, Pointer(GetOrdProp(Instance, PropInfo)), PropInfo^.PropType^);
    else
      raise EPropertyConvertError.CreateResFmt(@SInvalidPropertyType,
       [PropInfo.PropType^^.Name]);
    end;
      end;
    end;
      

  9.   

    哈哈,终于搞定,看了看typinfo的源码,才发现需要用setobjectprop 来设置Tstrings
    ,原来getpropvalue可以得到tkclass,但是setpropvalue只能设置value,不能设置对象。
    要用setobjectprop,就可以了。 把上面我贴的中间那句替换成:
       SetObjectProp(Components[i],'Items',mylist);就ok了,多谢linzhisong的提示,以及各位DX的帮助。