Reputation: 12864
I have a form with nearly 20 Textbox and 5 Combobox and one control in dependent on the other, Now I want to write the code for the form in such a way that, Pressing Enter Key and Tab Key should have the same functionality.
Like on pressing Tab Key the focus moves to next control should also be performed when I press the Enter Key. Similarly when I press the Enter Key, there is some process code written in key press event but this should also be performed when I press the Tab Key.
Upvotes: 4
Views: 37500
Reputation: 11
You can set the KeyPreview property of the form to True in the form's Load event and handle the KeyDown event of the form. Then, you can check if the active control is a text box and the pressed key is the Enter key, and if so, you can set the Handled property of the event to True and use the SelectNextControl method to move the focus to the next control in the tab order (example uses Form1).
Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Then add this code:
Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles myBase.KeyDown
If TypeOf Me.ActiveControl Is TextBox AndAlso e.KeyCode = Keys.Enter Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End Sub
This should allow the Enter key to act as a Tab key when the focus is on a text box in the form.
Upvotes: 1
Reputation: 77
This is quite old but I got here because I wanted to do the same thing. The problem with some of the answers here is that they will always jump to the next control when pressing enter and I only want it to do that with text boxes. If they get to a button, I want them to be able to hit enter to "click" that button. So here is what I did.
Private Sub txtName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtName.KeyPress, txtAttn.KeyPress, txtAdd1.KeyPress, txtAdd2.KeyPress, txtCity.KeyPress, txtState.KeyPress, txtZip.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
SendKeys.SendWait("{TAB}")
End If
End Sub
It is kind of a pain having to add all of the .keypress in the handles part of the sub but then you can control which items will cause it to move to the next control and which will not. Of course you also have to set the tab stops order at design time for this to work. But with this method, it can still trigger a button press once it tabs to a button and they hit enter again.
I would have just added this as a comment but I do not have enough points to add comments. :)
Upvotes: -1
Reputation: 11
I was able to accomplish this without having to manually create or set event handlers for each control. At the initialization of the form, I run a function that loops through every control and adds a generic handler function.
Private Sub AddHandlers()
Try
'Get the first control in the tab order.
Dim ctl As Windows.Forms.Control = Me.GetNextControl(Me, True)
Do Until ctl Is Nothing
If TypeOf ctl Is System.Windows.Forms.TextBox Or TypeOf ctl Is System.Windows.Forms.ComboBox _
Or TypeOf ctl Is System.Windows.Forms.CheckBox Or TypeOf ctl Is System.Windows.Forms.DateTimePicker Then
AddHandler ctl.KeyDown, AddressOf ReturnKeyTabs
End If
'Get the next control in the tab order.
ctl = Me.GetNextControl(ctl, True)
Loop
Catch ex As Exception
End Try
End Sub
Private Sub ReturnKeyTabs(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = System.Windows.Forms.Keys.Return Then
e.Handled = True
e.SuppressKeyPress = True
End If
ReturnKeyTabs(e.KeyCode)
End Sub
Private Sub ReturnKeyTabs(ByVal KeyCode As System.Windows.Forms.Keys)
If KeyCode = System.Windows.Forms.Keys.Return Then
System.Windows.Forms.SendKeys.Send("{Tab}")
KeyCode = 0
End If
End Sub
Upvotes: 1
Reputation: 51
A better option that I uses for the same problem is to create a new textbox class textboxClass and paste following code in its keypress event
Private Sub commonTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If Char.IsControl(e.KeyChar) Then
If e.KeyChar.Equals(Chr(Keys.Return)) Then
Me.Parent.SelectNextControl(Me, True, True, False, True)
e.Handled = True
End If
End If
End Sub
Now we can add any number of textbox to any form. it will behave as desired. when enter is pressed on the last textbox focus goes to first one.
This code as taken from @Mark Hall for single textbox from this page only.
Upvotes: 0
Reputation: 111
First Make your Form's Keypreview property= True And Then Paste The Code Below in Form's Keydown Event
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, True, True, True, False) 'for Select Next Control
End If
Upvotes: 11
Reputation: 119
simply make following function
Public Sub perform_tab_on_enter(ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
SendKeys.Send("{TAB}")
else
exit sub
End If
e.SuppressKeyPress = True 'this will prevent ding sound
End Sub
call this function on control's keydown event
Upvotes: 1
Reputation: 54532
The way that I have accomplished it in Winforms is by using the SelectNextControl
Method.
i.e.
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim tb As TextBox
tb = CType(sender, TextBox)
If Char.IsControl(e.KeyChar) Then
If e.KeyChar.Equals(Chr(Keys.Return)) Then
Me.SelectNextControl(tb, True, True, False, True)
e.Handled = True
End If
End If
End Sub
If you are using WPF you can use TraversalRequest
i.e.
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
Dim tb As TextBox
tb = CType(sender, TextBox)
If e.Key = Key.Return Then
tb.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))
ElseIf e.Key = Key.Tab Then
Exit Sub
End If
End Sub
As far as intercepting the Tab Key take a look at this Stackoverflow question.
Upvotes: 7