I need assistance for trying to get the name of a dynamically created button using and OnClick event in Delphi.
I am then want to use the name of that button and store it in a global variable.
This is where I am currently:
procedure TMap.FormShow(Sender: TObject);
var
btnCache : TButton;
begin
btnCache := TButton.Create(imgAerial);
with btnCache do
begin
onclick := ClickButton;
end;
procedure TMap.ClickButton(Sender: TObject);
begin
//Code for getting the name of the button
end;
btnCachein the form, not in this private procedure.ClickButtonis called,Senderis the button that was clicked, so just use a local variable of typeTButton, and assign it the value ofSenderwith a typecast likeMyBtn := (Sender as TButton);. You can then useMyBtnin the code as usual. If your code depends on the string name of the button, you've designed your code wrong.