开发一个自定义组件,执行组件的某一事件,可以打开一个窗口.窗口的操作要用到组件的属性,问题是如何能得到组件的属性?

解决方案 »

  1.   

    uses TypInfo;procedure TForm1.Button1Click(Sender: TObject);
    var
      vPropList: PPropList;
      I: Integer;
    begin
      for I := 0 to GetPropList(TObject(Sender), vPropList) - 1 do begin
        ListBox1.Items.Add(vPropList[I].Name);
      end;
    end;
      

  2.   

    [自定义窗口]
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Edit1: TEdit;
        procedure FormCreate(Sender: TObject);
      private
      myparentcomponent:Tobject;
        { Private declarations }
      public
      constructor Create(AOwner: TComponent;parentcomponent:Tobject); 
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}constructor TForm2.Create(AOwner: TComponent; parentcomponent: Tobject);
    begin
      inherited create(aowner);
      myparentcomponent:=parentcomponent;end;procedure TForm2.FormCreate(Sender: TObject);
    begin
    if myparentcomponent<>nil then
    if myparentcomponent is  Tbutton thenedit1.Text :=  (myparentcomponent as  Tbutton).Caption;
    end;end.[假设在一个Tbutton组件中打开一个窗口]
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    uses unit2;
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    form2:=Tform2.Create(self,sender);
    form2.Show;end;end.如果我想的没有错误,这个应该是个方法
      

  3.   

    你将组件当成 form的 parent 传进去,然后
    在 form 中访问检查它的Parent是不是 该组件就可吧????
      

  4.   

    zswangII(伴水清清)(一贴不灌,何以灌天下?) 我要的是在窗口中的过程如何得到组件的属性.
      

  5.   

    to aiirii(ari-爱的眼睛) 
    窗口的parent不能赋予组件的,你可以试验一下!to  qwertyasd(昊) 
    我的方法,应该能满足您的想法,try~~
      

  6.   

    procedure tmybutton.setoption(Sender: TObject);
    begin
      if reprintediterfrm=nil then reprintediterfrm:=treprintediterfrm.Create (reprintediterfrm,rep);
      reprintediterfrm.showmodal;
    end;
    ----
    上面是我的控件按你的方法需要用户设置Sender的属性,
    就这样
    mybutton1.setoption(mybutton1);能不能不用Sender的参数
      

  7.   

    procedure tmybutton.setoption(Sender: TObject);
    begin
      if reprintediterfrm=nil then reprintediterfrm:=treprintediterfrm.Create (reprintediterfrm,Sender);
      reprintediterfrm.showmodal;
    end;
    ----
    上面是我的控件按你的方法需要用户设置Sender的属性,
    就这样
    mybutton1.setoption(mybutton1);能不能不用Sender的参数
      

  8.   

    procedure tmybutton.setoption;
    begin
      if reprintediterfrm=nil then reprintediterfrm:=treprintediterfrm.Create (reprintediterfrm,Sender);
      reprintediterfrm.showmodal;
    end;
    -----------
    如上面如果控件的setoption方法不用sender参数, 
    reprintediterfrm:=treprintediterfrm.Create (self,Sender);
    这句如何写?
      

  9.   

    >>窗口的parent不能赋予组件的,你可以试验一下!
    还是不懂!!!在 
    procedure tmybutton.setoption;
    中, 你传入 self , 就是tmybutton的实例阿!然后,你在 
    reprintediterfrm 中,就可 if reprintediterfrm.parent is tmybutton then
            tmybutton(reprintediterfrm.parent).XX := YY;
        ...
      

  10.   

    zswangII(伴水清清)(一贴不灌,何以灌天下?) 我要的是在窗口中的过程如何得到组件的属性.
    **********************
    你要的是属性的名称还是属性的值?伴水的方法是取得属性的名称的
      

  11.   

    to  aiirii(ari-爱的眼睛)
    问题需要在事件中调用一个[新窗口],您将这个控件赋予[这个窗口]可以吗?try~~ :)
      

  12.   

    uses TypInfo;function FindProperty(AInstance: TObject; sPropertyName: String): Boolean;
    var
      PropList: PPropList;
      ClassTypeInfo: PTypeInfo;
      ClassTypeData: PTypeData;
      i: integer;
    begin
      Result := False;
      ClassTypeInfo := AInstance.ClassType.ClassInfo;
      ClassTypeData := GetTypeData(ClassTypeInfo);
      if ClassTypeData.PropCount <> 0 then
      begin
        GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
        try
          GetPropInfos(AInstance.ClassInfo, PropList);
          for i := 0 to ClassTypeData.PropCount - 1 do
            if (PropList[i]^.PropType^.Kind <> tkMethod)
              and (UpperCase(PropList[i]^.Name) = UpperCase(sPropertyName)) then
            begin
              Result := True;
              Break;
            end;
        finally
          FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
        end;
      end;
    end;function FindPropertyEx(AInstance: TObject; sPropertyName: String): Boolean;
    begin
      try
        GetOrdProp(AInstance, sPropertyName);
        Result := True;
      except
        Result := False;
      end;
    end;function SetPropValueEx(AInstance: TObject; APropName: String;
      AValue: Variant): Integer;
    begin
      try
        //if FindPropertyEx(AInstance, APropName) then  //编译运行找不到属性时可能会出现异常
        if FindProperty(AInstance, APropName) then      //调试时不会出现异常
        begin
          SetPropValue(AInstance, APropName, AValue);
          Result := 0;
        end
        else
          Result := -1;
      except
        Result := -2;
      end;
    end;
      

  13.   

    呵呵,你就把TMyButton作为全局变量或窗体的属性赋值过去~~
      

  14.   

    或者用TComponent.Tag属性传递TMyButton实例~~
    用RTTI的方法读取TMyButton实例的属性值~~
      

  15.   

    问题解决了;
    下面代码是tmybutton的setoption方法.
    procedure tmybutton.setoption();
    begin
      if reprintediterfrm=nil then reprintediterfrm:=treprintediterfrm.Create(self,self);
      reprintediterfrm.showmodal;
    end;
    下面是窗口代码
    constructor Treprintediterfrm.Create(AOwner: TComponent; parentcomponent: Tobject);
    begin
      inherited create(aowner);
      myparentcomponent:=parentcomponent;
    end;
    procedure Treprintediterfrm.FormCreate(Sender: TObject);
    begin
    if myparentcomponent<>nil then
    if myparentcomponent is  tmybuttonreprint then
    exit1.Text :=  (myparentcomponent as  tmybuttonreprint).text;
    end;
      

  16.   

    谢谢 aiirii(ari-爱的眼睛) 
         wnhoo(e梦缘)这是萦绕我很久的问题,
    在控件的方法中调用一个[新窗口],然后将这个控件赋予[这个窗口].
      

  17.   

    to qwertyasd(昊) 
    我上面其实早就给了您的方法
    constructor Create(AOwner: TComponent;parentcomponent:Tobject); parentcomponent:Tobject可以是任意的一个对象实例哦,hehe~~
      

  18.   

    问题解决
    if reprintediterfrm=nil then reprintediterfrm:=treprintediterfrm.Create(reprintediterfrm,self);
    这样就ok了,运行期和设计期都可使用
      

  19.   

    aiirii(ari-爱的眼睛) 
         wnhoo(e梦缘)
    非常感谢二位!
    请到下面接分
    http://expert.csdn.net/Expert/topic/2949/2949593.xml?temp=.7556421