Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify which button in array was clicked 1

Status
Not open for further replies.

Dooda

IS-IT--Management
May 14, 2001
76
GB
I create buttons dynamically and need to know which one has been clicked and which button's submenu has been invoked. Cannot find an easy solution, is there one or if not what is the best difficult approach?
Creation code is as follows (old program so it's Delphi4).
Code:
var
  btnMain: array [1..19] of TButton;
  stlButtonID: TStringList;  //IDs are read into here

for i := 0 to stlButtonID.Count-1 do
  begin
    btnMain[i] := TButton.Create(Self);
    btnMain[i].Parent := Self;
    btnMain[i].Name := stlButtonID[i];
    btnMain[i].PopupMenu := mnuButtons;
    ....and so on
 
I don't have the code I wrote that does this to hand!
But generally if you make the buttons part of a new class of objects, then each objectcan create its own button and popup menu etc.
There are even some built in functions that will automate popup menu creation for you and populate the menus.

Then you don't have to worry about which one belongs to each button.

Sorry I don't have time to go into more detail!!


Steve [The sane]: Delphi a feersum engin indeed.
 
Are you invoking the sub-menu by right-clicking on the button?

I first looked at using a common OnClick event for all of the buttons, but right-clicking doesn't fire the OnClick event for a button.

Roo
Delphi Rules!
 
If you assign a procedure to the OnClick event of the TButton control (the proc could be the same for all), the Self parameter of the Click event is assigned to the caller.

RC
 
Thanks to all for the helpful comments. I avoided creating classes etc as that's always tricky for ex COBOL and assembler folk!

The solution for button OnClick events was to set the Tag value for each button when created, then identify the calling button using [tt]TButton(Sender).Tag[/tt].

The Popup Menu was harder because it's owned by the form, meaning that there was no identification of the calling button. So a MouseDown event handler was created for the buttons and this fires before the popup menu when the right button is clicked. This handler stores the [tt]TButton(Sender).Tag[/tt] for use in events triggered by the popup menu.
 
Star for job well done! (And for posting your soution!)

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top