About Events

Seems there is some lack of clarity about event handlers and event definitions and how this affects 2019r2.

Lets start with event handlers and event definitions.

When you take a control, like a push button, and drag one onto a layout you are creating an instance of a button on that layout. And you can add event handlers to that control.

The reason you can add those event handlers is because the base class for push buttons has several event definitions. Those that are defined you can add to the instance. Any that are not you cant. Its that simple.

Custom subclasses of controls behave the same. An author might add new event definitions for events that make sense for their control.

So what happens when that author has implemented an event like “Open” and you drag an instance to the layout ?

Simple. You won’t be able to add that event to that control because the author implemented it so its no longer available for you to add when you put an instance on a layout.

So how can an author rectify this so you can once again add code to their custom controls Open event ? By adding an event definition in their custom control also called Open and raising that event in the event handler for the Open event.

What then happens at runtime is that the subclass implementation of the Open Event Handler is called and, as part of that code, it raises the Open event which will call any code added in the instance on the layout.

Lets see this in action

  1. create a new desktop project
  2. drag a Generic Button from the library all the way over to the Navigator & drop it into the Navigator. This will create a new class called CustomPushButton
  3. Now add the Event Handler for the Open event in this new subclass of PushButton (note if you’re using 2019r2 then you would add the Opening event)
  4. Now drag our new Class onto Window1 so you get an instance on the layout

Your project should look something like this

If you now select the instance on the Window and try to add an event you will see that the Open Event is not in the list.

So lets fix that

Select the CustomPushButton in the Navigator. Add an Event Definition and name it Open (in 2019r2 name it Opening)

Then in the CustomPushButtons Open event handler put

raiseevent Open // in 2019r2 put raiseEvent Opening

Now if you select the instance of the control on Window1 you CAN add the Open event (in 2019r2 you can add the Opening event)

IN fact when we added the Open, or Opening, event definition we could have named it ANYTHING we wanted.