Defining and handling custom events in wx.NET

In wx.NET, events will be processed according to the description "Event handling overview" of the wxWidgets manual - with two exceptions:

In order to cut a long story short, I will provide only one example here addressing both points. Please refer to the various sample applications to get a picture on how to deal with menus, buttons, and that stuff that of course produces events.

Consider that you want to use the event system to defer certain actions (refer to wx.TreeCtrl for a situation motivating such a procedure). Some actions are forbidden in events reacting on mouse moves. So, they shall be conducted afterwards.

Defining an event class:
Events in wxWidgets need a unique ID and a class holding the properties of the event that will be used by the event handler. These classes must inherit from wx.Event. In case that the new event shall be propagated like command events, the class shall inherit from wx.CommandEvent.
      class CustomCommandEvent : wx.CommandEvent
      {
          public static readonly int EVT_ID = wx.Event.AddEventType(typeof(CustomCommandEvent));
 
          public CustomCommandEvent()
              : base(EVT_ID)
          {
          }
 
          public CustomCommandEvent(IntPtr wxobj)
              : base(wxobj)
          {
          }
      }
The static method wx.Event.AddEventType() registers the new class as an event class and returns a new and unique ID for the new event type. Please note, that event classes MUST HAVE a CTor accepting instances of System.IntPtr poiting at native C++ instances of wxWidgets events.
Lets assume, that all the following will take place in the class FancyWindow inheriting from wx.Window.

Handlers for events:
Event handlers are methods of a certain signature: An object representing the sender of the event (typically an instance of wx.EvtHander) and the event that shall be handled. Usually, the first step is to cast the event type to the expected event type, to read relevant event properties, and to trigger the requested actions. The following example handles the new event type:
 void OnCustomCommandEvent(object sender, wx.Event evt)
 {
      CustomCommandEvent cevt=evt as CustomCommandEvent;
      if (cevt != null)
      {
          ...
          Actions
          ...
      }
 }
As described above, this event will be caused by mouse events to conduct actions that are not available on processing mouse events. A corresponding method to handle such an event looks like the following: wx.Window.AddPendingEvent() will add the new event at the end of the event queue.
 void OnMouse(object sender, wx.Event evt)
 {
      wx.MouseEvent mevt=evt as wx.MouseEvent;
      if (mevt != null)
      {
         ...
         some available action
         ...
      }
      // also run deferred action
      this.AddPendingEvent(new CustomCommandEvent());
 }
Configuration of the event table:
Finally, the methods handling events must be declared in the CTor of FancyWindow in order to enable wx.NET to identify and call these methods if appropriate.
 FancyWindow(wx.Window parent, int id, Drawing.Point pos, Drawing.Size size, wx.WindowStyles styles)
   : base(parent, id, posm size, styles)
   {
      ...
      #region Event Table
      this.EVT_MOUSE_EVENTS(new wx.EventHandler(this.OnMouse));
      this.AddEventListener(OnCustomCommandEvent.EVT_ID, new wx.EventHandler(this.OnCustomCommandEvent));
      #endregion
      ...
   }
The first method EVT_MOUSE_EVENTS is a method provided by wx.EvtHandler that declares handlers for mouse events. Class wx.EvtHandler defines a lot of these methods. The name is directly derived from the name of the wxWidgets macro that is used to populate event tables when using C++. Alle these methods on special events use the general methods wx.EvtHandler.AddEventListener or wx.EvtHandler.AddCommandRangeListener - the latter is for command events also specifying a range of relevant command IDs. Both methods additionally require the ID of the custom event. As mentioned above, we assigned this ID to a static class variable of the class representing the properties of the new event.

Manual of the wx.NET   (c) 2003-2011 the wx.NET project at   Get wx.NET at SourceForge.net. Fast, secure and Free Open Source software downloads