Obtaining the component´s name from an TObject

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Obtaining the component´s name from an TObject

Postby Anonimus » Sun Oct 03, 2004 8:10 pm

I try to develop a pocket calculator like the one´s in Windows Accesories, so I had design a form with so many buttons as operands and operators can be added to a simple calculator. But my nigthmare beggins when I try to figure out how can I write only one event OnClick manager for all buttons, since when I write code in only one event , as example in button1OnClick, this works only for that button and nothing else; I want to avoid to write code for each OnClick event, therefore I search if there´s a way to obtain the component´s name from the parameter TObject *Sender from the OnClick event. Is correct this way, or should I use the event OnClick for each button?
Anonimus
 

Using functions

Postby 2ffat » Wed Oct 06, 2004 6:40 am

I too would be interested in how one can obtain a component's name. However, I have a possible solution to your problem. Just create a function that all the OnClick events call. You still have to create an event for each button's OnClick but then you call your function. Something like:
Code: Select all
void __fastcall TMyForm::1BitBtnClick(TObject *Sender)
{
    MyFunction();
}
//---------------------------------------------------------------------------
James P. Cottingham

Look at me still talking
when there is science to do.
User avatar
2ffat
Forum Mod
Forum Mod
 
Posts: 433
Joined: Wed Jun 23, 2004 7:07 am
Location: South Hill, VA

My way

Postby Guest » Thu Oct 07, 2004 10:04 am

Here is a slightly different alternative that I've used many times.

Try using the TObject *Sender parameter. It points to the button you clicked! For the first button specify an event handler (ButtonCommonClick below) as OnClick and code it after this model:
Code: Select all
void __fastcall TForm1::ButtonCommonClick(TObject *Sender)
  {
  if (Sender == Button1)  // Button1 is the name of the button
    {
    Label1->Caption = "Button1";  // Just an example
    }
  else if (Sender == Button2)  // Button2 is the name of the button
    {
    Label1->Caption = "Button2";  // Just an example
    }
  else if (Sender == Button3)  // Button3 is the name of the button
    {
    Label1->Caption = "Button3";  // Just an example
    }
  }

Then you paste the name ButtonCommonClick (or the name your used above) as OnClick for all the other buttons. It saves a bit of typing!

If you really need the component's name you can get it this way
Code: Select all
  TButton *bu;
  bu = dynamic_cast<TButton*>(Sender);
  if (bu)
    {
    Label1->Caption = bu->Name;
    }


Regards,
Anders
Guest
 

Cool!

Postby 2ffat » Thu Oct 07, 2004 10:36 am

Cool![/b] I've been wondering how to to that.
James P. Cottingham

Look at me still talking
when there is science to do.
User avatar
2ffat
Forum Mod
Forum Mod
 
Posts: 433
Joined: Wed Jun 23, 2004 7:07 am
Location: South Hill, VA

Obtaining the component´s name from an TObject

Postby Anonymus » Thu Oct 07, 2004 12:38 pm

Thanks to both of you. The Ander´s code bu = dynamic_cast<TButton*>(Sender); woks exactly I need! using it, I just code only one event´s OnClick code and attach to the rest of buttons OnClick handlers using the object inspector window, saving a lot of typing.Thanks again.
Anonymus
 

Postby Guest » Fri Oct 22, 2004 4:42 am

I use this function to go through all the components on a scrollbox and then set enabled of the specific control...

//Parse SpecialFields;
TControl *Control;
for(int n=0; n<ScrollBox1->ControlCount; n++){
Control = ScrollBox1->Controls[n];
if(SpecialFields.find(Control->Name.c_str(),0)<SpecialFields.length()){
Control->Enabled = false;
}
}
Guest
 


Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 21 guests

cron