![]() | ![]() Products ![]() ![]() ![]() ![]() |
Home » Technical Support » Elevate Web Builder Technical Support » Support Forums » Elevate Web Builder General » View Thread |
Messages 1 to 9 of 9 total |
![]() |
Sun, Feb 26 2017 8:41 PM | Permanent 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 AM | Permanent 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 AM | Permanent 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 AM | Permanent 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 AM | Permanent 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 AM | Permanent 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 AM | Permanent 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 AM | Permanent 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 PM | Permanent 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 |
This web page was last updated on Thursday, March 23, 2023 at 02:07 AM | Privacy Policy![]() © 2023 Elevate Software, Inc. All Rights Reserved Questions or comments ? ![]() |