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 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.
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