小弟在FORM中有若干个组件,请问如何判断各组件是否有Caption属性,若有的把NAME放到一个ListBox中呢?万望各位大哥帮忙。

解决方案 »

  1.   

    If Label1.Caption <> "" Then
            Text1.Text = Label1.Caption
        End If
      

  2.   

    你可以按Ctrl+F1查看当前组件的帮助中的Properties看是否有此属性,或者更简单的你直接些代码些上组建的名称后面打"."看是否有此属性
      

  3.   

    感谢两位大哥的帮忙,但小弟是需要在运行时用程序去判断,Form内的所有组件哪一个有Caption属性,如有就将该组件的名称加到ListBox中,请各位指点指点。
      

  4.   

    IF Form1.组件名.Caption <> "" then
       ListBox1.Add.Items(Form1.组件名);
      

  5.   

    var PropInfoPtr:PPropInfo;
        i:integer;
    begin
      for i:=0 to componentcount-1 do
        begin
          PropInfoPtr:=GetPropInfo(components[i],'caption');
          if PropInfoptr<>nil then
            begin
              listbox1.Items.Add(components[i].Name);
            end
        end;
      

  6.   

    需要
    uses 
        TypInfo
      

  7.   

    zhlwyy(海龙)大哥,误会我的意思了。在我的FORM中有TDBEdit,TButton,TLabel,TListBox等组件,我是希望,程序自动遍历FORM中所有的组件,当发现该组件有Caption这个属性时(如TDBEdit是没有Caption属性的,如果我遍历时程序运行到"Form.Dbedit1.Caption"的话,程序是会出错并告诉我该组件没有这个属性的,我就是想加入异常或判断去消除这错误信息),将这个组件的名称放到ListBox中。请大哥指点指点。
      

  8.   

    If Label1.Caption <> "" Then
            Text1.Text = Label1.Caption
        End If我是不是看错了,这个是Pascal代码?
      

  9.   

    同意hongqi162(失踪的月亮.net)的
    implementationUses TypInfo;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to ComponentCount - 1 do
        if IsPublishedProp(Components[I], 'Caption') then
          ListBox1.Items.Add(Components[I].Name);
    end;