Keys missing as designer property of UC

Viewed 57

I'm starting to code a custom keyboard for my company, and I'm not sure why I don't have access to all keys in the designer. This seems like it should be very simple. One example key is Escape, but there are others.

New Project ->
right-click -> add new -> UserControl

UserControl1.vb

Public Class UserControl1
    Private _myKey As Keys
    Public Property MyKey() As Keys
        Get
            Return _myKey
        End Get
        Set(ByVal value As Keys)
            _myKey = value
        End Set
    End Property
End Class

Build -> Form1 designer -> Drop UserControl1 onto Form1.vb

Form1.vb [Design]
->Select UserControl11 Properties -> Misc -> MyKey

try to set to Keys.Escape (or any number of others).

The available keys list is fairly verbose, but absolutely not complete.

What is the right way to do this? I would much prefer find a solution that allows me to do this via the designer than having to do this on load programmatically.

The below works, but again, I don't understand why I can't do this in the designer:

Public Class Form1

Public Sub New()
        InitializeComponent()
        UserControl11.MyKey = Keys.Escape
    End Sub
End Class

I know I can define my own Enum and get it, but that's so much time for something that should probably just work with .net. What am I doing wrong here?

2 Answers

First off, great question with clear repro directions.

It's strange behavior for sure. Maybe someone has an explanation.

This workaround might do the trick though

Public Class UserControl1

    Private _keyCode As Integer

    Public Property KeyCode As Integer
        Get
            Return _keyCode
        End Get
        Set(ByVal value As Integer)
            _keyCode = value
        End Set
    End Property

    Public Property MyKey As Keys
        Get
            Return CType(_keyCode, Keys)
        End Get
        Set(ByVal value As Keys)
            _keyCode = value
        End Set
    End Property

End Class

You can set KeyCode in the property pages to the integer value of Escape in System.Windows.Forms.Keys for instance, which is 27.

enter image description here

at which point, MyKey actually displays Escape. Not sure why it doesn't show up in the list.

The values can be found on MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.keys(v=vs.71).aspx

Note: you can also just type Escape in MyKey. Adding KeyCode gives a nice other option though.

If you look at the System.Windows.Forms.Keys Enum in a decompiler, you will see that it is decorated with both a TypeConverter and Editor attribute. These attributes are controlling what you can do in the WinForms designer as they are inherited by your MyKey property.

<Flags, TypeConverter(GetType(KeysConverter)), Editor("System.Windows.Forms.Design.ShortcutKeysEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(UITypeEditor)), ComVisible(True)> _
Public Enum Keys

If you want to present the Keys Enum with the default Enum UI, you will need to decorate your property with its own TypeConverter and Editor attributes that tell the designer to use the default editor and the Enum type converter (this converter allows conversion between the Enum and String representation) instead of the ones declared on the Keys type.

<System.ComponentModel.TypeConverterAttribute(GetType(System.ComponentModel.EnumConverter))>
<System.ComponentModel.Editor(GetType(UITypeEditor), GetType(UITypeEditor))>
Public Property MyKey() As Keys
Related