如何知道FORM 中某个组件是否具有某个属性.在表单里面,我放了各种组件,如(DBEdit,DBGrid,DBMemo,Label,Button...),我想通过组件遍历的方式扫描,查找这个容器中(FORM内)的每个组件是否有某个属性如(DataSet属性)
如何做?只写了一部分. 如何判断某个组件是否有DataSet属性
    for i:=0 to Form1.ComponentCount-1 do
    begin
       // if 如果Components[i] 有DataSet这个属性  then
       //   begin 
       //     .....
       //   end
       // else     
       //   begin 
       //     .....
       //   end;        
    end;

解决方案 »

  1.   


    不好意思,更正,是DataSource属性,不是DataSet属性.
      

  2.   

    如何知道FORM 中某个组件是否具有某个属性.在表单里面,我放了各种组件,如(DBEdit,DBGrid,DBMemo,Label,Button...),我想通过组件遍历的方式扫描,查找这个容器中(FORM内)的每个组件是否有某个属性如(DataSource属性)
    如何做?只写了一部分. 如何判断某个组件是否有DataSource属性
        for i:=0 to Form1.ComponentCount-1 do
        begin
           // if 如果Components[i] 有DataSource这个属性  then
           //   begin 
           //     .....
           //   end
           // else     
           //   begin 
           //     .....
           //   end;        
        end;
      

  3.   

    只能判断他是否属于某个类
    if components[i] is TDBedit 
    then ...知道了她是否属于某个类也就知道了她是否具有某个属性
      

  4.   

    USES typinfo;  {Add this to your USES statement.}var
      i: integer;
    begin
      for i := componentCount - 1 downto 0 do
        with components[i] do
        begin
          if GetPropInfo(ClassInfo, 'font') <> nil  then
            font.size := (NewFormWidth DIV OldFormWidth) * font.size;
        end;
    end;
      

  5.   

    有DataSource属性的总共有几个类嘛。
    选中DataSource,按F1看一下。
    罗列抄写一遍咯。
    按 mzg3(radom) 的方法一个个去判断,我想也就这个方法最好理解了。
      

  6.   

    另外,不知道MethodAddress('DataSource')有没有用?
      

  7.   

    使用rtti的信息
    IsPublishedProp(Components[i], 'DataSource')
    别忘了uses typInfo这个单元
      

  8.   

    http://blog.codelphi.com/nil/archive/2004/06/18/14857.aspx
      

  9.   

    uses TypInfo;
    {ClassName:对象名;PropName:属性名}
    Function Is_ExistProp(ClassName:TObject;PropName:String):Boolean;
    var
      PropInfo:PPropInfo;
    begin
      result:=Flase;
      PropInfo:=GetpropInfo(ClassName.ClassInfo,PropName);
      if PropInfo<>nil then
        restult:=True
    end;
      

  10.   


        那么如何设置这个属性的值呢?    for i:=ControlCount-1 downto 0 do
        begin
           if GetPropInfo(Controls[i], 'DataSource')<>nil  then
           begin
              //如何设置这个DataSource的属性为一个已存在的TDataSource的对象(如 
              // DataSource1)
              //SetVariantProp(Controls[i],'DataSource',DataSource1);
              //上面这条好象,编译报错.
           end;
        end
      

  11.   

    GetPropInfo(Controls[i].ClassInfo, 'DataSource')<>nil
      

  12.   

    var
      PropInfo:PPropInfo;
    ……
    PropInfo:=GetpropInfo(Controls[i].ClassInfo,'DataSource');
    if PropInfo<>nil then
      setObjectProp(Controls[i].ClassInfo,PropInfo,DataSource1);
      

  13.   

    搞错了
    应该是
    var
      PropInfo:PPropInfo;
    ……
    PropInfo:=GetpropInfo(Controls[i].ClassInfo,'DataSource');
    if PropInfo<>nil then
      setObjectProp(Controls[i],PropInfo,DataSource1);
      

  14.   

    uses
      TypInfo;
    ...
    procedure SetDataSource(Comps: array of TControl; DS: TDataSource);
    var
      Loop: Integer;
      PropInfo: PPropInfo;
    begin
      for Loop := Low(Comps) to High(Comps) do
      begin
        { Get info record for DataSource property }
        PropInfo := GetPropInfo(Comps[Loop].ClassInfo, 'DataSource');
        { If property exists, set value to DS }
        if Assigned(PropInfo) then
          SetOrdProp(Comps[Loop], PropInfo, Longint(DS));
      end;
    end;
    ...
    Table1.Open;
    SetDataSource([DBEdit1, DBText1, DBNavigator1], DataSource1);