delphibuttonautocompletebuttonclick

Delphi Autocomplete ButtonClick Procedure


So when you double click on a button and it autocompletes the buttonclick procedure, I'm curious about how the compiler knows which button the function is linked to. For example it would make TForm1.Button1Click(Sender: TObject);

So how does the compiler know which button that is linked too? Does it just parse the procedure name to see what button it is?


Solution

  • You can name the method to any name, and Delphi doesn't parse or use the method name to identify the component or event associated.

    If you do it at design time, the event association with a event handler is stored in the DFM file, where you find something like:

      object Button1: TButton
        Left = 104
        Top = 64
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
    

    The OnClick = Button1Click makes your program associate the method (also known as the event handler) to the event (a special kind of property) of the object at runtime, when the form is created.

    You can also associate an event to any compliant method at runtime, for example with this code:

    type
      TForm1 = class(TForm)
        Button1: TButton;
      private
        procedure MyClick(Sender: TObject);
        procedure MyOtherClick(Sender: TObject);
    
    
      ....
    
    procedure TForm1.AssociateClickHandler;
    begin
      Button1.OnClick := MyClick;
    end;
    
    procedure TForm1.MyClick(Sender: TObject);
    begin
      Button1.OnClick := MyOtherClick; //from now, the other method with handle the event.
    end;
    

    Use any name you want

    At design time, you can write the name you want for the event handler directly in the ObjectInspector, then press Enter and Delphi will create the method with that name for you. If you don't provide a name, Delphi auto-generates the name for the method using the component name, plus the event name without the "On". If the method already exists, the IDE just associate the event with that method.

    Write the desired method name:

    enter image description here

    Press enter:

    enter image description here

    You can associate the same method to different events of the same object, or to the same event of different objects.

    For example, you can associate the MyOwnMethodName shown above to the OnClick of any number of buttons. Usually, the Sender parameter contains a reference to the object that is firing the event.