Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array of TBitBtn's onclick event handler

37 views
Skip to first unread message

James Charest

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to
Hi,
I have created an array of BitBtns at run time. I have not been
able to determine which of these buttons were clicked in the onclick
event handler. Any help would be appreciated.


TBitBtn *MyBitBtn[20];


for(int i=0;i < 20; i++)
{
MainForm->MyBitBtn[i] = new TBitBtn(this);
MainForm->MyBitBtn[i]->Parent = this;
MainForm->MyBitBtn[i]->OnClick = MyBitBtnClick;
MainForm->MyBitBtn[i]->Left = 8;
MainForm->MyBitBtn[i]->Top = hgt;
hgt = hgt + 25;
}

void __fastcall TMainForm::MyBitBtnClick(TObject *Sender)
{
How to tell which BitBtn was clicked?
}

Thanks

Jim Charest


james.charest

unread,
Aug 3, 2000, 3:00:00 AM8/3/00
to
Well I found the answer in the book Teach yourself C++ Builder in 14
days. The Sender parameter is used so you can have one even handler
handle for than one componenet. It may not be the best but it works

for(int i=0;i<20;i++)
{

if(Sender = = MainForm->MyBitBtn[i])
{
strcpy(string,MainForm->MyBitBtn[i]->Caption.c_str());

Remy Lebeau

unread,
Aug 3, 2000, 3:00:00 AM8/3/00
to

james.charest <james....@smtp.snet.net> wrote in message
news:3989670F...@smtp.snet.net...

> It may not be the best but it works

Sender returns a pointer to the specific TBitBtn that triggered the event,
you don't need to iterate through your array looking for it.

void __fastcall TMyForm::MyBitBtnClick(TObject *Sender)
{
// Sender is a TObject*, but we need a TBitBtn*
TBitBtn *btn = dynamic_cast<TBitBtn*>(Sender);

// is Sender really a TBitBtn* ?
if( btn != NULL)
{
// yes, get the Caption
strcpy(string, btn->Caption.c_str());
}
}


Gambit


James Charest

unread,
Aug 3, 2000, 3:00:00 AM8/3/00
to
Thanks for the help!
0 new messages