Event Handlers

From RAD Studio
Jump to: navigation, search

Go Up to About the Object Inspector Index


An event handler is a method containing code that is executed in response to an event.

Event-based execution programming sequence:

  1. The application waits for an event to happen. For example, when the user clicks a button.
  2. The event triggers the event handler. For example, the application calls the OnClick event handler for the button.
    OnClick Event Handler.png
  3. The code inside the event handler is executed.
For Delphi:
 //Unit.pas
 
 (...)

 type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject); //Onclick event declaration
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 (...)


 procedure TForm1.Button1Click(Sender: TObject);
   
 begin
     //Code to be executed
 end;
For C++:
 // Unit.h

 (...)

 class TForm1 : public TForm
 {
 __published:	// IDE-managed Components
	TButton *Button1;
	void __fastcall Button1Click(TObject *Sender); //Onclick event declaration
 private:	// User declarations
 public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
 };

 (...)
 
 //Unit.cpp

 void __fastcall TForm1::Button1Click(TObject *Sender)
 {
    //Code to be executed
 }

Using the Object Inspector to Generate Event Handlers

Use the Object Inspector to add component event handlers to the code.

The Events page of the Object Inspector shows published events for the selected component from the form.

Double-click the Value column of the Events page to generate a procedure header and the corresponding structure on the code.

See Also