Search code examples
vb.netdelphiequivalenthandles

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?


Solution

  • 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;