Say we had something like
Private Sub ClickObject(ByVal sender As System.Object, ByVal e as System.Eventargs)
Handles Object1.click, Object2.click, Object3.click
Which takes the event after the 'Handles' and sends them to the function.
Is there an equivalent for this in Delphi, and how would I do it?
Add a TActionList to your form. Add a TAction to it and handle its OnExecute event as you would the OnClick event of some other control. Assign the Action properties of the controls to refer to the action you added to the action list. (This also causes the controls to acquire their captions and enabled and visible properties from the associated action. It's meant to make it easier to have menus and toolbar buttons have uniform states when they represent the same command.)
Yes.
You can create an event handler and assign it to multiple controls.
procedure TForm1.ThreeControlsClick(Sender: TObject);
begin
if Sender = Button1 then
HandleButton1Click
else if Sender = ComboBox1 then
HandleComboBox1Click
else if Sender = Edit1 then
HandleEdit1Click;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.OnClick := ThreeControlClick;
ComboBox1.OnClick := ThreeControlClick;
Edit1.OnClick := ThreeControlClick;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With