Control and Component Designers Part Four – Events

This is a quick addendum to part three. In that section we looked at un-hiding some properties in the standard controls which were implemented in .NETCF SP2. As well as adding Fore and Back colour support, SP2 introduced support for a few more events which were declared in the original classes but never fired. Specifically these include the keyboard events KeyDown, KeyUp and KeyPress. Just like the colour properties these can be added to our design time assembly so that we can hook these events from the designer.

Description(“Occurs when a key is first pressed.”)]
public new event KeyEventHandler KeyDown;

Description(“Occurs after a user is finished pressing a key.”)]
public new event KeyPressEventHandler KeyPress;

Description(“Occurs when a key is released.”)]
public new event KeyEventHandler KeyUp;

We declare the events “new” so that they will replace the base class implementation, but as this is design time only, the code generated hooks the events that already exist in the Control class. The category and description attributes improve the grouping in the property pane.

The last event item to cover is the DefaultEventAttribute which can be applied to a class. In our PictureBoxEx class we inherit from the standard PictureBox (Thanks to Sergey Bogdanov for submitting the code). Because I have now added a Click event to the designer (In SP2 the PictureBox supports the Click event) we can make this the default event for the class, so that when you double click on the control in the designer it will automatically rig up a handler for you and drop you into the appropriate place in the code. This makes a little more sense that the PictureBox for which the default event is ParentChanged.

    public class PictureBoxEx : System.Windows.Forms.PictureBox
    public class PictureBoxEx : System.Windows.Forms.PictureBox, OpenNETCF.Windows.Forms.IWin32Window

Part 1 | Part 2 | Part 3 | Part 4

Published by Peter Freeman Foot

Microsoft Windows Development MVP