Board index » cppbuilder » Identifying dynamically created buttons

Identifying dynamically created buttons


2005-10-26 10:10:01 PM
cppbuilder43
Hello,
I managed to dynamically create new buttons on Form1 with:
TButton *pBtn=new TButton(Form1);
Form1->InsertControl(pBtn);
pBtn->Left=10;
pBtn->Top=10;
But how do I identify each individual dynamically created button when
mouseclicking it?
I heard some rumours about 'dynamic casting'.
Does anybody have any example?
thanks in advance,
Henk
 
 

Re:Identifying dynamically created buttons

I think that one of the easiest things to do is to use the Tag property.
Set it to something unique and create a case statement based on that.
 

Re:Identifying dynamically created buttons

On Wed, 26 Oct 2005 16:10:01 +0200, Henk van Winkoop wrote:
Quote
But how do I identify each individual dynamically created button when
mouseclicking it?
Why do you want to identify them? What do you need to do that
requires knowing /which/ button?
--
liz
 

{smallsort}

Re:Identifying dynamically created buttons

"Henk van Winkoop" < XXXX@XXXXX.COM >wrote:
Quote
I managed to dynamically create new buttons on Form1 with:
[snip]
But how do I identify each individual dynamically created
button when mouseclicking it?

I heard some rumours about 'dynamic casting'.
Does anybody have any example?
First you assign an OnClick method to the buttons. Its parameter
is of type TObject*. You need to cast it to access the button's
properties. If only buttons call this method you can use static
casting. If other controls invoke this method, too, than you
need dynamic casting.
TButton *pBtn=new TButton(Form1);
Form1->InsertControl(pBtn);
pBtn->Left=10;
pBtn->Top=10;
pBtn->Caption = "My Button";
pBtn->OnClick = ButtonClick;
Either:
void __fastcall TForm1::ButtonClick(TObject *Sender)
{
TButton* pButton = static_cast<TButton*>(Sender);
ShowMessage(pButton->Caption);
}
Or:
void __fastcall TForm1::ButtonClick(TObject *Sender)
{
TButton* pButton = dynamic_cast<TButton*>(Sender);
if(pButton)
ShowMessage(pButton->Caption);
}
To identify a button, use either its Caption as in my example or
its Tag as Ron suggests or use its address (but in that case you
would have to store the pointer instead of using a local pointer
variable when creating the button).
 

Re:Identifying dynamically created buttons

I want to create a kind of visual programming utility for kids.
So creating blocks ('if'-blocks, 'else'-blocks, 'while'-blocks, etc.) and
moving them around and glueing them together by mouse.
So a kid 'programmer' must move blocks around by clicking with the mouse on
it.
I thought I use bitbuttons as blocks adding bitmaps on them. (Or I could use
panels or whatever)
When a bitbutton is clicked it must be moved, recoloured or unglued or
whatever.
Thats why I need to identify each separate block.
Henk
"liz" < XXXX@XXXXX.COM >schreef in bericht
Quote
On Wed, 26 Oct 2005 16:10:01 +0200, Henk van Winkoop wrote:

>But how do I identify each individual dynamically created button when
>mouseclicking it?

Why do you want to identify them? What do you need to do that
requires knowing /which/ button?

--
liz
 

Re:Identifying dynamically created buttons

"Henk van Winkoop" < XXXX@XXXXX.COM >wrote in message
Quote
Form1->InsertControl(pBtn);
You should be using the Parent property instead:
pBtn->Parent = Form1;
Quote
But how do I identify each individual dynamically created
button when mouseclicking it?
The Sender parameter of all the event handlers that you assign to the Button
will point to the Button. For example:
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
TButton *pBtn = new TButton(this);
pBtn->Parent = this;
pBtn->Left = 10;
pBtn->Top = 10;
//...
pBtn->OnClick = ButtonClicked;
}
void __fastcall TForm1::ButtonClicked(TObject *Sender)
{
TButton *pBtn = static_cast<TButton*>(Sender);
// use pBtn as needed...
}
Quote
I heard some rumours about 'dynamic casting'.
You use a dynamic_cast only when you are not sure what class type a
polymorphic pointer is pointing to. For example:
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
TButton *pBtn = new TButton(this);
pBtn->Parent = this;
pBtn->Left = 10;
pBtn->Top = 10;
//...
pBtn->OnClick = ButtonClicked;
TBitBtn *pBitBtn = new TBitBtn(this);
pBitBtn->Parent = this;
pBitBtn->Left = pBtn->Left + pBtn->Width;
pBitBtn->Top = pBtn->Top;
//...
pBitBtn->OnClick = ButtonClicked;
}
void __fastcall TForm1::ButtonClicked(TObject *Sender)
{
TButton *pBtn = dynamic_cast<TButton*>(Sender);
if( pBtn )
{
// use pBtn as needed...
}
else
{
TBitBtn *pBitBtn = dynamic_cast<TBitBtn*>(Sender);
if( pBitBtn )
// use pBitBtn as needed...
}
}
Gambit
 

Re:Identifying dynamically created buttons

"Henk van Winkoop" < XXXX@XXXXX.COM >wrote in message
Quote
When a bitbutton is clicked it must be moved, recoloured
or unglued or whatever. Thats why I need to identify
each separate block.
That is what the Sender parameter in the event handlers is for. See my
other reply for an example.
Gambit
 

Re:Identifying dynamically created buttons

It's fully understood,
thanks
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schreef in bericht
Quote

"Henk van Winkoop" < XXXX@XXXXX.COM >wrote in message
news:435f8d83$ XXXX@XXXXX.COM ...

>Form1->InsertControl(pBtn);

You should be using the Parent property instead:

pBtn->Parent = Form1;

>But how do I identify each individual dynamically created
>button when mouseclicking it?

The Sender parameter of all the event handlers that you assign to the
Button
will point to the Button. For example:

__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
TButton *pBtn = new TButton(this);
pBtn->Parent = this;
pBtn->Left = 10;
pBtn->Top = 10;
//...
pBtn->OnClick = ButtonClicked;
}

void __fastcall TForm1::ButtonClicked(TObject *Sender)
{
TButton *pBtn = static_cast<TButton*>(Sender);
// use pBtn as needed...
}

>I heard some rumours about 'dynamic casting'.

You use a dynamic_cast only when you are not sure what class type a
polymorphic pointer is pointing to. For example:

__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
TButton *pBtn = new TButton(this);
pBtn->Parent = this;
pBtn->Left = 10;
pBtn->Top = 10;
//...
pBtn->OnClick = ButtonClicked;

TBitBtn *pBitBtn = new TBitBtn(this);
pBitBtn->Parent = this;
pBitBtn->Left = pBtn->Left + pBtn->Width;
pBitBtn->Top = pBtn->Top;
//...
pBitBtn->OnClick = ButtonClicked;
}

void __fastcall TForm1::ButtonClicked(TObject *Sender)
{
TButton *pBtn = dynamic_cast<TButton*>(Sender);
if( pBtn )
{
// use pBtn as needed...
}
else
{
TBitBtn *pBitBtn = dynamic_cast<TBitBtn*>(Sender);
if( pBitBtn )
// use pBitBtn as needed...
}
}


Gambit