Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB [(Function) handles ...] equivalent in Delphi

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?

like image 622
Skeela87 Avatar asked May 31 '26 23:05

Skeela87


2 Answers

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.)

like image 101
Rob Kennedy Avatar answered Jun 03 '26 12:06

Rob Kennedy


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;
like image 27
Ken White Avatar answered Jun 03 '26 12:06

Ken White