I am using .NET 3.5 framework of VB.NET 2008.
I have some textboxes in my form. I want the tab-like behavior when my user presses ENTER on one of my textboxes. I use
I had the same problem and I could not make this answer work on Framework 2.0 so I dug deeper.
You would have to first handle the PreviewKeyDown on the textbox so when ENTER came along you would set IsInputKey so that it could be handled by or forwarded to the keyDown event on the textbox. Like this:
Private Sub txtFiltro_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles txtFiltro.PreviewKeyDown
Select Case e.KeyCode
Case Keys.Enter
e.IsInputKey = True
End Select
End Sub
and then you would handle the event keydown on the textbox. One of the answer was on the right track but missed setting the e.IsInputKey.
Private Sub txtFiltro_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFiltro.KeyDown
If e.KeyCode = Keys.Enter Then
e.handled = True
Textbox1.Focus()
End If
End Sub
Use this code it will work OK. You shall click on TextBox1 and then go to event and select Keyup and double click on it. You wil then get the lines for the SUB.
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
MsgBox("Fel lösenord")
End If
End Sub
Make sure the form KeyPreview property is set to true.
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
There is no need to set the KeyPreview Property to True. Just add the following function.
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) _
As Boolean
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
SendKeys.Send("{Tab}")
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Now, when you press Enter on a TextBox, the control moves to the next control.
I see this has been answered, but it seems like you could avoid all of this 'remapping' of the enter key by simply hooking your validation into the AcceptButton on a form. ie. you have 3 textboxes (txtA,txtB,txtC) and an 'OK' button set to be AcceptButton (and TabOrder set properly). So, if in txtA and you hit enter, if the data is invalid, your focus will stay in txtA, but if it is valid, assuming the other txts need input, validation will just put you into the next txt that needs valid input thus simulating TAB behaviour... once all txts have valid input, pressing enter will fire a succsessful validation and close form (or whatever...) Make sense?
Use KeyDown Event instead of KeyPress
If e.KeyCode = Keys.Enter Then
MsgBox ("You pressed enter")
End if
Note: Make sure you don't have ACCEPT BUTTON set on your form. AcceptButton set it to 'none'