creating new buttons && view the one that's clicked


I'm creating a whole bunch of buttons during run-time. When anyone of these buttons is clicked it should go through the same function, but with different parameters. A small example to clarify;

for (x = 0; x < 10; x++)
TButton *b = new TButton(this);
b->Caption = "My button " + IntToStr(x);
b->Top = 100;
b->Left = 100 + 10 * x;
b->OnClick = myfunction(x);
b->Parent = this;

void __fastcall TForm1::myfunction(int x)
ShowMessage("Button " + IntToStr(x) + " clicked");

This, however, doesn't work. I get an "Not an allowed type" error when compiling. Apparantly I can't pass variables like this to myfunction.

I can only do something like this;
b->OnClick = myfunction;

void __fastcall TForm1::myfunction(TObject *Sender)
ShowMessage("Button ? clicked");

But how do I know which of the ten buttons is clicked now? How can I pass this information to the function?



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion