Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread How to check which dynamically created TButton was clicked?
Sun, Feb 26 2017 8:41 PMPermanent Link

kamran

Hi

Following on from my previous post.

I have a number of TButtons populated with menu captions at runtime and stored in an TObjectlist
called "MenuButtons" and placed on a panel called "HeaderMenuPanel" ( This bit is fine up to here )

I have saved the button captions in a TStringList called "HeaderCaptions" and the index of these in a TStringlist
called "HeaderCaptionsIndex" while they were being created in the hope that I could make use of these in detecting which button was pressed. Not so sure if that is useful !!

How then to detect/capture  which dynamically created TButton was clicked/selected ?

I thought it was a good idea to do this dynamically but it seems to be more of a headache!

cheers


Kamran
Mon, Feb 27 2017 1:12 AMPermanent Link

Michael Dreher

kamran wrote:

 // How then to detect/capture  which dynamically created TButton was clicked/selected ?

One method distinguishing components is to assign the "Tag" or "Name" properties suitable values, which can be accessed in event handlers.

M. Dreher
Mon, Feb 27 2017 2:49 AMPermanent Link

Uli Becker

> I have a number of TButtons populated with menu captions at runtime and stored in an TObjectlist
> called "MenuButtons" and placed on a panel called "HeaderMenuPanel" ( This bit is fine up to here )

What are you doing with the ObjectList?

> I have saved the button captions in a TStringList called "HeaderCaptions" and the index of these in a TStringlist
> called "HeaderCaptionsIndex" while they were being created in the hope that I could make use of these in detecting which button was pressed. Not so sure if that is useful !!

Actually you don't need any ObjectLists oder StringList when you create
the buttons in code.

As Michael indicated (and I told you before) you can assign an ID (of a
dataset e.g.) to the button's tag.
And there is no need to store the button's caption one more time. It's
just there.

When creating the buttons, assign the caption, the tag and an event
handler "OnClick".
In the OnClick event handler you can get either the caption or the tag:

CurrentID := TButton(Sender).Tag;
CurrentCaption := TButton(Sender).Caption;

Uli
Mon, Feb 27 2017 4:05 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> Actually you don't need any ObjectLists oder StringList when you create the buttons in code.

But if you are storing a list of objects, and want to refer to one from the event handler, then storing the index of the relevant object in the Tag property will help there too. Saves a complex lookup later.

--

Matthew Jones
Mon, Feb 27 2017 6:33 AMPermanent Link

kamran

Thanks for the tips; Uli and Michael

I will dig further to understand tags and their use in such situations.

Cheers

Kamran

Uli Becker wrote:

> I have a number of TButtons populated with menu captions at runtime and stored in an TObjectlist
> called "MenuButtons" and placed on a panel called "HeaderMenuPanel" ( This bit is fine up to here )

What are you doing with the ObjectList?

> I have saved the button captions in a TStringList called "HeaderCaptions" and the index of these in a TStringlist
> called "HeaderCaptionsIndex" while they were being created in the hope that I could make use of these in detecting which button was pressed. Not so sure if that is useful !!

Actually you don't need any ObjectLists oder StringList when you create
the buttons in code.

As Michael indicated (and I told you before) you can assign an ID (of a
dataset e.g.) to the button's tag.
And there is no need to store the button's caption one more time. It's
just there.

When creating the buttons, assign the caption, the tag and an event
handler "OnClick".
In the OnClick event handler you can get either the caption or the tag:

CurrentID := TButton(Sender).Tag;
CurrentCaption := TButton(Sender).Caption;

Uli
Mon, Feb 27 2017 9:18 AMPermanent Link

kamran

Hi

I am doing this type of runtime stuff for the first time and its a little frustrating
so I am extremely grateful for the guidance here.

So far:

1. created menubuttons in runtime - all ok

2. saved the tag value of each new button created to access the button later
-----------------------------------------------------------
var MenuHeaderIndex:integer;
begin
 NewButton := TButton.Create(HeaderMenuPanel);
 MenuButtons.Add(NewButton);
 MenuHeaderIndex := MenuButtons.Add(NewButton);
 NewButton.Tag :=MenuHeaderIndex; // allow it to be identified in code
-------------------------------------------------------------------------

3. created a new HeaderButtonOnClick procedure

procedure TMenuFrm.HeaderBtnOnClick(Sender: TObject);
var CurrentID:Integer;
   CurrentCaption:string;
begin
 ShowMessage('HeaderButtonOnClick');
 CurrentID := TButton(Sender).Tag;
 CurrentCaption := TButton(Sender).Caption;
end;

At this stage I am not sure which component to attach this to

for now I have attached this new HeaderButtonOnClick procedure to the HeaderMenuPanel
as this exists in the design already - see below.

-----------------------------------------------------------
procedure TMenuFrm.HeaderMenuPanelClick(Sender: TObject);
begin
 HeaderMenuPanel.OnClick:=HeaderBtnOnClick;
end;
-----------------------------------------------------------

But attaching to a the relevant button click "would make better sense" if i knew how ?

4. So in the new HeaderButtonOnClick procedure I wish to detect which
button was pressed and take appropriate action.

The error is "Object doesn't support property or method  tbuttontrol_getcaption"


So still not having much luck.

Thanks


Kamran



kamran wrote:

Thanks for the tips; Uli and Michael

I will dig further to understand tags and their use in such situations.

Cheers

Kamran

Uli Becker wrote:

> I have a number of TButtons populated with menu captions at runtime and stored in an TObjectlist
> called "MenuButtons" and placed on a panel called "HeaderMenuPanel" ( This bit is fine up to here )

What are you doing with the ObjectList?

> I have saved the button captions in a TStringList called "HeaderCaptions" and the index of these in a TStringlist
> called "HeaderCaptionsIndex" while they were being created in the hope that I could make use of these in detecting which button was pressed. Not so sure if that is useful !!

Actually you don't need any ObjectLists oder StringList when you create
the buttons in code.

As Michael indicated (and I told you before) you can assign an ID (of a
dataset e.g.) to the button's tag.
And there is no need to store the button's caption one more time. It's
just there.

When creating the buttons, assign the caption, the tag and an event
handler "OnClick".
In the OnClick event handler you can get either the caption or the tag:

CurrentID := TButton(Sender).Tag;
CurrentCaption := TButton(Sender).Caption;

Uli
Mon, Feb 27 2017 9:24 AMPermanent Link

Matthew Jones

kamran wrote:

> But attaching to a the relevant button click "would make better sense" if i knew how ?

The easiest way to work this stuff out is to do it in the IDE first, and then delete things when no longer needed. Or use another project.

In your code that is creating the buttons, that's the place to assign the OnClick:

 NewButton := TButton.Create(HeaderMenuPanel);
 NewButton.OnClick := HeaderBtnOnClick;

Assuming of course that the button has the OnClick. You can manually create an event handler, or create one in the IDE and use that.

--

Matthew Jones
Mon, Feb 27 2017 10:26 AMPermanent Link

Uli Becker

Matthew already told you how to use the event handler.
In addition I atached a small sample project. It shows you how to deal
with this kind of stuff.

Uli



Attachments: Sample Buttons.zip
Mon, Feb 27 2017 7:58 PMPermanent Link

kamran

Hi Uli and Matthew thanks to you both.

Uli .. the "sample project" is very much appreciated..

Its a great help.

Kamran


Uli Becker wrote:

Matthew already told you how to use the event handler.
In addition I attached a small sample project. It shows you how to deal
with this kind of stuff.

Uli
Image