Reputation: 2006
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 used the following code:
Private Sub txtDiscount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDiscount.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
But it doesn't work for me.
What is the solution?
Upvotes: 31
Views: 381753
Reputation: 1448
Someone suggested "PreviewKeyDown". Given this code and results entering '1 2 tab 3' one can see that "PreviewKeyDown" can be used only to redirect a key (that would normally not pass thru "KeyDown/Press/Up") to it, so you can act upon it. Note that the key is NOT added to "Text". You can only remember having seen the key in Preview and then act upon this saved information in KeyDown/Up/Press
Dim eDotIsInputKey As Boolean = False
Private Sub AkteNr_TextChanged(sender As Object, e As EventArgs) Handles AkteNr.TextChanged
Debug.WriteLine("ch !" + AkteNr.Text + "!")
End Sub
Private Sub AkteNr_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles AkteNr.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
e.IsInputKey = True
End If
eDotIsInputKey = e.IsInputKey
Debug.WriteLine("pr " + CStr(e.KeyCode) + " " + CStr(e.IsInputKey))
End Sub
Private Sub AkteNr_KeyDown(sender As Object, e As KeyEventArgs) Handles AkteNr.KeyDown
Debug.WriteLine("kd " + CStr(e.KeyCode) + " " + CStr(eDotIsInputKey))
End Sub
Result if entering 1 then 2 then tab then 3:
pr 49 False
kd 49 False
ch !1!
pr 50 False
kd 50 False
ch !12!
pr 9 True
kd 9 True
pr 51 False
kd 51 False
ch !123!
Upvotes: 0
Reputation: 412
I have test this code on Visual Studio 2019
Its working superb
Just Paste it into you form code
it will work on all textboxs on same form
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim keyCode As Keys = CType(msg.WParam, IntPtr).ToInt32
Const WM_KEYDOWN As Integer = &H100
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter _
AndAlso Me.ActiveControl.GetType.Name = "TextBox" Then
Me.SelectNextControl(Me.ActiveControl, True, True, False, True)
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Upvotes: 0
Reputation: 181
Private Sub SomeTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles SomeTextBox.KeyPress
If Asc(e.KeyChar) = 13 Then
MessageBox.Show("Enter pressed!")
e.Handled = True
End If
End Sub
Upvotes: 4
Reputation: 111
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'
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 1
The following code will work.
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(13) Then
MsgBox("enter key pressd ")
End If
End Sub
End Clas
Public Class Form1
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox("enter key pressd ")
End If
End Sub
End Class
Upvotes: 0
Reputation: 93
use this code this might help you to get tab like behaviour when user presses enter
Private Sub TxtSearch_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TxtSearch.KeyPress
Try
If e.KeyChar = Convert.ToChar(13) Then
nexttextbox.setfoucus
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Upvotes: 0
Reputation: 51
Private Sub BagQty_KeyPress(sender As Object, e As KeyPressEventArgs) Handles BagQty.KeyPress
Select e.KeyChar
Case Microsoft.VisualBasic.ChrW(Keys.Return)
PurchaseTotal.Text = Val(ActualRate.Text) * Val(BagQty.Text)
End Select
End Sub
Upvotes: 0
Reputation: 49
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
Upvotes: 1
Reputation: 2999
You can use PreviewKeyDown Event
Private Sub txtPassword_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles txtPassword.PreviewKeyDown
If e.KeyCode = Keys.Enter Then
Call btnLogin_Click(sender, e)
End If
End Sub
Tested on VB.NET 2010
Upvotes: 7
Reputation: 39
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?
Upvotes: 3
Reputation: 131
I'm using VB 2010 .NET 4.0 and use the following:
Private Sub tbSecurity_KeyPress(sender As System.Object, e As System.EventArgs) Handles tbSecurity.KeyPress
Dim tmp As System.Windows.Forms.KeyPressEventArgs = e
If tmp.KeyChar = ChrW(Keys.Enter) Then
MessageBox.Show("Enter key")
Else
MessageBox.Show(tmp.KeyChar)
End If
End Sub
Works like a charm!
Upvotes: 10
Reputation: 51
also can try this:
If e.KeyChar = ChrW(Keys.Enter) Then
'Do Necessary code here
End If
Upvotes: 3
Reputation: 2191
In the KeyDown Event:
If e.KeyCode = Keys.Enter Then
Messagebox.Show("Enter key pressed")
end if
Upvotes: 42
Reputation: 2006
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.
Upvotes: 13
Reputation:
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
Upvotes: 28