问题:我现在根据业务的增加减少会自动生成一些控件,但是这些控件的响应函数如何编写?
举个例子,现在有三个业务,我动态生成了三个button,那么对应的三个onclick事件如何编写?
while(...)
 begin 
  button:=TButton.create(self);
  button.left:=10;
  button.top:=100;
  ....
 end;我点击这些button的事件该放在哪里?

解决方案 »

  1.   

    首先在你的TForm类中定义一过程:
    type
      TForm1 = class(TForm)
       ...
      private
        { Private declarations }
      public
        { Public declarations }
        procedure test(Sender: TObject) ; //这里
      end;
    在implementation下写出它的实现如:
    procedure TForm1.test(Sender: TObject) ;
    begin
      Showmessage('ok');
    end;
    然后:
     button.OnClick:=test;就可以了
      

  2.   

    如果想传递参数怎么办?test(Sender: TObject)无法传递参数阿
      

  3.   

    根据不同的事件写不同的事件过程如:OnClick事件   
          procedure ButtonClick(Sender: TObject); 
        OnMouseDown事件
          procedure ButtonMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        这样可以根据需要传递参数。(上面过程的Button、Shitf、X、Y都可以传递)
      

  4.   

    传参数
    1、从TButton继承一个新的TNewButton,增加属性,存放你的变量
    2、建立数组,把参数放到数组里,然后根据Button的tag读取数组中的相应信息
      

  5.   

    Sender: TObject 这个不是参数吗?
    如你个更明显的例子:改上面的test为
      procedure test(Sender: TObject;var Key: Char); //这里
    ...
    procedure TForm1.test(Sender: TObject;var Key: Char);
    begin
      Showmessage(Key);
    end;
    然后设
    button.OnKeyPress:= test;
    你选中新的button,按一键,就会显示该键
    ---------------------------------------------
    不同的事件有不同的参数.看一下help文档就知道了
      

  6.   

    楼主,可以考虑挡截Windows消息中WM_COMMAND,然后在分别相应事件。
      

  7.   

    问题已经解决,我新建了一个类aaa,这个类有几个属性,这些属性用来存放需要传递的参数,在create中被加载,然后定义一个myClick(sender:TObject)用来传递