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.
- wx.NET does not use event tables. Instead, you will declare event handlers as delegates typically in the CTor of the window class providing a method for event handling. Refer to the example below. However, the resulting code still looks quite similar to the C++ original.
- Additionally, custom events will be declared differently. This is due to the fact, that .NET does not use CPP macros to create event IDs. All declarations are done by methods and static initializers. Again, the example provided by this section might be useful.
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.
- You have to declare a new event type. the code presented here will also use a custom event class.
- You have to implement method handling the event. the sample will introduce two handlers, one for the mouse event initiating the pending event and one for the pending event itself.
- The event handler providing the methods for handling the events (typically a wx.Window) must configure its event table. This is usually done by the CTor of the handler.
Lets assume, that all the following will take place in the class
- 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.
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.
class CustomCommandEvent : wx.CommandEvent
public static readonly int EVT_ID = wx.Event.AddEventType(typeof(CustomCommandEvent));
public CustomCommandEvent(IntPtr wxobj)
FancyWindow inheriting from
- 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:
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 OnCustomCommandEvent(object sender, wx.Event evt)
CustomCommandEvent cevt=evt as CustomCommandEvent;
if (cevt != null)
void OnMouse(object sender, wx.Event evt)
wx.MouseEvent mevt=evt as wx.MouseEvent;
if (mevt != null)
some available action
- 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.
The first method
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.AddEventListener(OnCustomCommandEvent.EVT_ID, new wx.EventHandler(this.OnCustomCommandEvent));
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.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
(c) 2003-2011 the wx.NET project at