tags:

views:

171

answers:

2

I am using 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 doen't work for me.

Please give me a solution.

A: 

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
eschneider
the handler is on a textbox, not the form...
Mitch Wheat
KeyPreview Property is set. But nothing happened!!!
Tareq
This works for me; but you must have the KeyPreview set to true...
eschneider
+1  A: 

I found the solution. For help of others I add the solution.

There is no need to set the KeyPreview Property is 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.

Tareq
That's almost the same as my method, except at a lower level. Did you even try my code? It does work...
eschneider