Board index » cppbuilder » How can I assign my event handler function of the dynamicaly created vcl component

How can I assign my event handler function of the dynamicaly created vcl component

How can I assign my event handler function of the dynamicaly created vcl
component...
for example:

void __fastcall TForm1::FormDblClick(TObject *Sender)
{
void __fastcall MyFun(TObject*);

  TButton *MyButton;  
  MyButton = new TButton(this);
  MyButton->Parent = this;
  MyButton->Caption = "Say Hello";
  MyButton->OnClick = MyFun;  /*  of course doesn't work, what should i
do ?
*/

Quote
}

/*-----------------------------------------------------------------------------------*/
void __fastcall MyFun(TObject *Sender){
  Form1->Label1->Caption = "Hello World...";
Quote
}

 

Re:How can I assign my event handler function of the dynamicaly created vcl component


Quote
Ozan Kyly?oelu <ozan.kilico...@eee.deu.edu.tr> wrote:
>How can I assign my event handler function of the dynamicaly created vcl
>component...

You're almost there; just make the event handler a method of your Form

Quote
>for example:

>void __fastcall TForm1::FormDblClick(TObject *Sender)
>{
>void __fastcall MyFun(TObject*);

This declaration goes to your header:

class TForm1 : public TForm
{
__published:    // IDE-managed Components
  TLabel *Label1;
  void __fastcall FormDblClick(TObject *Sender);
private:        // User declarations
  void __fastcall MyFun(TObject* Sender); // <------ there
public:         // User declarations
  __fastcall TForm1(TComponent* Owner);

Quote
};
>  TButton *MyButton;  
>  MyButton = new TButton(this);
>  MyButton->Parent = this;
>  MyButton->Caption = "Say Hello";

>  MyButton->OnClick = MyFun;

This is all gonna work, if you make this..:

Quote
>void __fastcall MyFun(TObject *Sender){
>  Form1->Label1->Caption = "Hello World...";
>}

...into a TForm1 method

void __fastcall TForm1::MyFun(TObject* Sender) {

  Label1->Caption = "Hello World...";

Quote
}

Good luck,

Hens Zimmerman

Other Threads