Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++RAD Studio

Learn C++ Event Handling In 5 Minutes

In this short tutorial, C++ Product Manager, David Millington, explains what event handlers are and how to use them in your C++ application development. 

Overview

  • The event is that something happens, such as a button being clicked
  • Event handler – a method that’s called when something happens so your code can respond to it
    • Technical details: an object-method pointer, referring to both the method and object instance on which to call the method. And it can have any signature.
    • Usually named On-something, such as OnPaint. The method assigned to a control’s OnPaint property will be called by that control when it paints.

Defining Event Handlers

In an event receiver class, such as your form, you define event handlers, which are simply methods that match the event handler type (return type, calling convention, and arguments) that match the event that they will handle. They are assignable like method pointers (since they are an object-oriented method pointer) to variables. Usually you do this in the Object Inspector, since events are heavily used for UI code: you will create an event handler (method) for (say) an OnClick event from the IDE, which will generate an empty method for you you can use to fill in the behaviour you want.

Code that handles events, ie sees something happen, just calls that method pointer to invoke your method.

Firing Events

To fire an event, simply check if the method pointer is null or not, and if not, call it. Usually you don’t need to worry about this: you define the methods and assign them, and existing library code, like a button class, looks after calling them.

Be sure to check out other tutorials on C++ Builder here:

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Software Developer | CS(CyberSec) Undergrad at APU Malaysia | Delphi/C++ Builder Enthusiast | Microsoft Learn Student Ambassador | Microsoft Azure Certified

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES