Library tutorials & articles

Intercepting keys in custom UITypeEditor's

The Problem & Solution

That's it.

I've written the ResourceImageEditor code.

I've created a sample MyPictureBox (derived from System.Windows.Forms.PictureBox ) overriding the Image property as to have the ResourceImageEditor specified as the property's type editor.

I've compiled the code, placed the MyPictureBox control onto a form and invoked the drop-down user interface...

The ImageResourceEditor in action

The mouse interface worked well. However, when I've selected an item with the keyboard and then pressed the ENTER key, the drop-down list disappeared, but my selection has been lost (i.e. the previously selected image hasn't changed). I've quickly discovered that when the ENTER key is pressed, the ListBox doesn't generate the KeyDown event.

The ESC key didn't generate the KeyDown event either, but it wasn't a problem because the drop-down list was "automagically" closed and I didn't have to process the currently selected item.

The property grid has apparently "stolen" the ENTER and ESC keys before the ListBox control could get a chance to process them. Or did it?

To make a long story short, the solution that worked was to employ the ProcessDialogKey method. The method is called during message preprocessing to handle dialog characters, such as TAB , RETURN , ESCAPE and also the arrow keys. The method is declared within the System.Windows.Forms.Control class in such a way that it simply delegates the call to the control's parent (if any). I've "subclassed" the ListBox control and I've overridden the ProcessDialogKey method to intercept the ENTER key like this:

Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
  If keyData = System.Windows.Forms.Keys.Return Then
    RaiseEvent EnterPressed(Me, EventArgs.Empty)
    Return True  ' True means we've processed the key
  Else
    Return MyBase.ProcessDialogKey(keyData)
  End If
End Function

Instead of generating the KeyDown event from within the ProcessDialogKey implementation, I've decided that a more straightforward approach would be to generate a new, distinguished event - the EnterPressed event. The ResourceImageEditor.EditValue implementation has been changed to handle this event (instead of the KeyDown ) event and everything finally worked correctly.

You can use this technique to intercept the ENTER key in any Control-derived class that you use for implementing the drop-down UI inside your type editor. For implementation details, please have a look at the source code in the solution accompanying this article.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Intercepting keys in custom UITypeEditor's.

Leave a comment

Sign in or Join us (it's free).

Palo Mraz I live in Slovakia with my wife, two sons (fulltime), one daughter (occasionally) and a dog. I've been doing Microsoft Windows development since 1988; primarily in VB. I'm a big fan of the MS .NET ...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

Want to stay in touch with what's going on? Follow us on twitter!