Using Alt+# Keys in VB.NET Win Forms

awdigrigoli

Member
Joined
Mar 30, 2005
Messages
12
Programming Experience
5-10
I would like to use Alt + # combinations to perform data actions and opening windows from a child form that open in the parent form. Could someone please give me some help?

Thank you, Anthony
 

shaheed

Member
Joined
Jun 3, 2004
Messages
20
Programming Experience
1-3
well i think to achieve your desired objective you might have to use all three key related events i.e. KeyUp, KeyDown and KeyPress. now, declare a global boolean variable lets say:
Private isAltKeyDown As Boolean
now, when any key is pressed down, KeyDown event will be fired. to detect if an Alt key is down pressed, place similar code in KeyDown event handler:
If e.KeyCode = Keys.Alt Then
isAltKeyDown = True
End If
while Alt key is pressed down if user presses any button, two events will be fired; KeyUp and KeyPress. In KeyPress you can detect the key pressed and perform the function accordingly by placing similar code in KeyPress event:
If isAltKeyDown Then
Select Case e.KeyChar
Case 'a':
....
End Select
End If
Now, if Alt or any key is released after pressing down then of course KeyUp event is fired so in KeyUp event place this single line of code:
If e.KeyCode = Key.Alt Then
isAltKeyDown = False
End If
well fellow i hope you will get some logic to work on this. i am not sure about the code because i am out of programming these days :) anyway, the basic need was to give you an idea.
 

sevenhalo

Well-known member
Joined
Oct 21, 2005
Messages
137
Location
Iowa
Programming Experience
3-5
You could also use the MainMenu control.

In the sub items, if you place a '&' before a letter of the context item, it will be Alt+ enabled.

ie. If you have a MainMenu control with File as one of your MenuItems, and under that item you have an Open Command, Change "Open" to "&Open"

If you press alt+O, the Open.Click event will be raised
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,334
Location
Sydney, Australia
Programming Experience
10+
Putting the ampersand in a MenuItem or a Label is called a mnemonic. To use a mnemonic assigned to a MenuItem that item must be visible. This means to select the Open item on a File menu you would have to press and hold Alt, then press F and then O. The alternative is to assign a key combination to the Shortcut property of a MenuItem. It cannot be just Alt+, but the MenuItem does not have to be visible for it to be activated.
 
Top Bottom