dino dino - 3 months ago 7
Vb.net Question

How can I check if a string of a key from a ComboBox is pressed in VB.NET?

I have a combobox in my form. It contains all these keys: BACKSPACE
BREAK
CAPS LOCK
DELETE
END
ENTER
ESC
HELP
HOME
INSERT
NUM LOCK
PAGE DOWN
PAGE UP
PRINT SCREEN
SCROLL LOCK
TAB
UP ARROW
DOWN ARROW
LEFT ARROW
RIGHT ARROW
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
F13
F14
F15
F16. For example when the key selected in the combobox is pressed do something. How can I do that?

Answer

There's a number of ways I could see this going. One would be to create an object with two attributes: one being the key name string and the other being the key itself. Then bind this to the combo box with the key name string as the displayed item.

Another would be to convert the text of your combobox to the key itself using this function:

Public Function KeyFromString(theKey As String) As Keys
    Dim kc As KeysConverter = New KeysConverter()
    Return CType(kc.ConvertFrom(theKey), Keys)
End Function

like so in whatever key pressed event you are using:

if e.key = KeyFromString(combobox.text) then
'do work
end if
Comments