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

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

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
}

I also try to make MyFun member function of the TForm1 [ TForm1::Myfun ]
it doesn't
work either...
Even if, Myfun as a member of TMyButton that dynamicaly created which
derived from the TButton
nothing has changed.

The only way that I can by pass this problem assigning event handler of
staticaly created TButton1's
OnClick event handler which contains my code inside...
  .
  .
  .
  MyButton->OnClick = Button1Click;
  .
  .
  .

it works... However this is not the way that I seek...

I guess, there is something related about "__closure"s that I
missunderstood.

Thanks in advance

 

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


Hi Ozan

This works for me:

int the .h file for the Form
//------------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
   //..... All the components
private: // User declarations
public:  // User declarations
   __fastcall TForm1(TComponent* Owner);
   __fastcall ~TForm1();

   void __fastcall MyButtonClick(TObject* Sender);

Quote
};

//------------------------------------------------------------------------------

int the .Cpp file:
//------------------------------------------------------------------------------
void __fastcall TForm1::MyButtonClick(TObject* Sender)
{
    // Your Code

Quote
}

//------------------------------------------------------------------------------
void __fastcall TForm1::FormDblClick(TObject *Sender)
{
  TButton *MyButton;
  MyButton = new TButton(this);
  MyButton->Parent = this;
  MyButton->Caption = "Say Hello";

  MyButton->OnClick = MyButtonClick;

Quote
}

//------------------------------------------------------------------------------

Good luck
Asger

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


Thanks Asger...
[ also thanks Hens Zimmerman ]

I resolve the problem with your code sample.
The problem was the misplaced fuction decleration in class defination of
Tform1 in my code...

thanks again...
and...
have a nice coding :)

Other Threads