Board index » cppbuilder » Create on click event at runtime

Create on click event at runtime


2006-07-07 05:43:34 PM
cppbuilder70
Hi,
I created an array of buttons at run time and I want to
add an on click event for each button of the array.
How can I do this?
Thanks in advantage.
Regards Enrico
 
 

Re:Create on click event at runtime

At 11:43:34, 07.07.2006, Enrico wrote:
Quote
Hi,
I created an array of buttons at run time and I want to
add an on click event for each button of the array.

How can I do this?
procedure TForm1.TheOnClickHandler(Sender: TObject);
var
Name: string;
begin
Name := (Sender as TButton).Name;
ShowMessage('You clicked button ' + Name);
end;
procedure TForm1.AddOnClickEvent;
var
I: Integer;
begin
for I := 0 to High(ButtonArray) do
ButtonArray[I].OnClick := TheOnClickHandler;
end;
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Few things are harder to put up with than a good example."
- Mark Twain (1835-1910)
 

Re:Create on click event at runtime

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
At 11:43:34, 07.07.2006, Enrico wrote:

>Hi,
>I created an array of buttons at run time and I want to
>add an on click event for each button of the array.
>
>How can I do this?

procedure TForm1.TheOnClickHandler(Sender: TObject);
var
Name: string;
begin
Name := (Sender as TButton).Name;
ShowMessage('You clicked button ' + Name);
end;

procedure TForm1.AddOnClickEvent;
var
I: Integer;
begin
for I := 0 to High(ButtonArray) do
ButtonArray[I].OnClick := TheOnClickHandler;
end;
Perhaps in c++ <g>
// check the syntax, it's been a while
const int numButtons(10);
std::vector<TButton*>Buttons;
Buttons.reserve(10);
for(size_t i = 0; i < numButtons; ++i) {
Buttons[i] = new TButton(this);
Buttons[i].tag = i;
Buttons[i].OnClick = TheOnClickHandler;
}
void __fastcall TheOnClickHandler(TObject* Sender) {
// get the tag from sender if you need to know
// which button it is
}
 

{smallsort}

Re:Create on click event at runtime

At 14:01:47, 07.07.2006, Duane Hebert wrote:
Quote

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >wrote in message
news:xn0eofydd4euiwm00a-velthuis@www.teamb.com...
>At 11:43:34, 07.07.2006, Enrico wrote:
>
>>Hi,
>>I created an array of buttons at run time and I want to
>>add an on click event for each button of the array.
>>
>>How can I do this?
>
>procedure TForm1.TheOnClickHandler(Sender: TObject);
>var
>Name: string;
>begin
>Name := (Sender as TButton).Name;
>ShowMessage('You clicked button ' + Name);
>end;
>
>procedure TForm1.AddOnClickEvent;
>var
>I: Integer;
>begin
>for I := 0 to High(ButtonArray) do
>ButtonArray[I].OnClick := TheOnClickHandler;
>end;

Perhaps in c++ <g>
LOL! I read too many groups. Sorry. Of course.
Syntax was correct, but it must be a closure, i.e.:
void __fastcall TForm1::TheOnClickHandler(TObject* Sender)
{
// get the tag from sender if you need to know
// which button it is
}
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Love is friendship set on fire." -- Jeremy Taylor
 

Re:Create on click event at runtime

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
LOL! I read too many groups. Sorry. Of course.
:-) I suspected as much.
Quote
Syntax was correct, but it must be a closure, i.e.:

void __fastcall TForm1::TheOnClickHandler(TObject* Sender)
{

// get the tag from sender if you need to know
// which button it is

}
Yep.
 

Re:Create on click event at runtime

Thanks a lot !!?
Enrico