Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOT to KNOW which BUTTON is pressure? 2

Status
Not open for further replies.

luistsousa

Technical User
May 12, 2003
66
PT
hello

If I put some buttons, and if all they execute the same function, after an event onclick, how that this function knows which the button that was pressured?


Many thanks

Luis
 
Howdy,

You can use the TObject *Sender pointer that is passed to event handlers to determain which button called the function. If you are calling another function within the event handler that also needs to know the button that was clicked, pass it the TObject *Sender pointer as an argument.

Then you can use a switch or if statement(s) to execute button specific code.

Hope I could help...
onrdbandit
 
Hi Again

I have mak e try to use the Sender with the code in down. However, the ShowMessage don't show the caption or the name of the button. There are some tip for this?

Regards


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
botao = new TButton(this);
botao->Parent = this;
botao->Caption = "one";
botao->Name="one";
botao->Left = 100;
botao->Top = 30+i*40;
botao->Visible = True;
botao->OnClick=click;
}

void __fastcall TForm1::click(TObject *Sender){
ShowMessage(AnsiString(TButton(Sender).Caption));
}

 
TButton(Sender) is your problem. This creates a temporary TButton (with Sender as the Owner) that will delete itself as soon as it goes out of scope. So of course its Caption property will be empty.

You wanna cast it to a TButton instead like so...

ShowMessage(((TButton*)Sender)->Caption);
 
HI Vorlath

Many thanks for Your tip. Work very well.
It is a big problem for me, resolved.

Regards
Luis A. Sousa
 
if(dynamic_cast<TButton *>(Sender) != 0)
{
if(dynamic_cast<TButton *>(Sender) == Button1)
do something
if(dynamic_cast<TButton *>(Sender) == Button2)
do something
if(dynamic_cast<TButton *>(Sender) == Button2)
do something
}

not quite as compact though.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top