-
Mar 21st, 2025, 07:48 PM
#1
Thread Starter
Addicted Member
[RESOLVED] function to capture keyboard events. Where do I find that?
I have a Form project with several combobox's on it. I need to catch the <ENTER> key.
How do I get the function generated, or do I need to create that myself?
EDIT: In VBA you could select the function of an item from a dropdown box and it would generate the code. Anything like this in VB Net?
Last edited by FunkMonkey; Mar 21st, 2025 at 07:53 PM.
-
Mar 21st, 2025, 09:18 PM
#2
Re: function to capture keyboard events. Where do I find that?
What do you want to do when your user presses Enter?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2025, 10:51 PM
#3
Re: function to capture keyboard events. Where do I find that?
There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.
The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
My usual boring signature: Nothing
 
-
Mar 21st, 2025, 11:00 PM
#4
Re: function to capture keyboard events. Where do I find that?
The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.
WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
-
Mar 22nd, 2025, 02:45 PM
#5
Thread Starter
Addicted Member
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by Shaggy Hiker
There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.
The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
Thanks Shaggy, but I really do wan to process the <ENTER> key. This is possible in VBA.
Code:
Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
Dim szMenuSelection As String
Dim nMenu As Integer
If KeyCode = vbKeyReturn Then
'clear our flag
AbortRun = False
nMenu = GetMenuEnumFromRunOption(RunOption)
szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, RunOption)
DoEvents
Call CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, RunOption)
DoEvents
End If
End Sub
I need to catch when the user presses the enter key, I ensure it is it and then I process a routing to start the process for the program.
Is there any way to create one if VB does not offer something similar?
-
Mar 22nd, 2025, 02:48 PM
#6
Thread Starter
Addicted Member
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by jmcilhinney
The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.
WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
Thanks JMC, I'll give this a try and see if I can get it working. Maybe Shaggy has a way as well.
-
Mar 22nd, 2025, 03:22 PM
#7
Thread Starter
Addicted Member
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by Shaggy Hiker
There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.
The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
Thanks Shaggy, but I really do need to capture the <ENTER> key. Here is my VBA code.
Code:
Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
Dim szMenuSelection As String
Dim nMenu As Integer
If KeyCode = Keys.Return Then
'clear our flag
SetAbortRun(False)
nMenu = GetMenuEnumFromGetRunOption()(GetRunOption())
szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, GetRunOption())
CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, GetRunOption())
End If
End Sub
The user needs to press enter in the combobox to initiate the event. It's not a change of the index, but the <ENTER> key which fires the process of the program.
-
Mar 22nd, 2025, 03:34 PM
#8
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by FunkMonkey
Thanks Shaggy, but I really do need to capture the <ENTER> key. Here is my VBA code.
Code:
Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
Dim szMenuSelection As String
Dim nMenu As Integer
If KeyCode = Keys.Return Then
'clear our flag
SetAbortRun(False)
nMenu = GetMenuEnumFromGetRunOption()(GetRunOption())
szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, GetRunOption())
CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, GetRunOption())
End If
End Sub
The user needs to press enter in the combobox to initiate the event. It's not a change of the index, but the <ENTER> key which fires the process of the program.
Here you would use the "e" parameter to check what key was pressed.
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
MessageBox.Show("Enter key pressed")
End If
End Sub
-
Mar 22nd, 2025, 03:53 PM
#9
Thread Starter
Addicted Member
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by jmcilhinney
The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.
WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
JMC, I tried to select the object -> open Properties, but in Properties is no place to select other handlers. I would show a picture but my screen captures do not wan to load. They look like they are uploading but nothing actually uploads, so I can't show a picture of where/what I'm doing. But the start of the comment indicates what I am doing.
Am I missing something?
EDIT: OK...... I found the handlers. I was looking in the wrong place. Thanks JMC.
Last edited by FunkMonkey; Mar 22nd, 2025 at 03:58 PM.
-
Mar 22nd, 2025, 03:58 PM
#10
Thread Starter
Addicted Member
Re: function to capture keyboard events. Where do I find that?
 Originally Posted by wes4dbt
Here you would use the "e" parameter to check what key was pressed.
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
MessageBox.Show("Enter key pressed")
End If
End Sub
Thanks wes, looks like what I need.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|