How to make ENTER key working like TAB key in Windows Forms (WinForms) Application using C# and VB.Net

Lama
 
on Dec 29, 2015 02:12 AM
Sample_183782.zip
13108 Views

Hello, I googled this but all solutions gave me errors, like:

 

 Private Sub Textbox1_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

The error : SelectNextControl is not a member of System.Web.UI.Control

And the following one:

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

gives a similar one for the line:   Me.SelectNextControl(tb, True, True, False, True)

 

This one:

 Private Sub Text1_KeyPress(ByVal KeyAscii As Integer)
        If KeyAscii = 13 Then  ' The ENTER key.
            SendKeys."{tab}")    ' Set the focus to the next control.
            KeyAscii = 0        ' Ignore this key.
        End If
    End Sub

 The error is : SendKeys is a type and cannot be used as expression

I replaced it with:

 SendKeys.Send("{tab}") 

but with no effect :(

 I used someone else, and told me that teextbox1.KeyPress is not found

 

I am using windows server 2008, visual basic 2008 version

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 29, 2015 03:46 AM
on Sep 12, 2017 06:42 AM

Hi Lama,

I have created sample that full-fill your requirement.

For Windows Application

You can do it in two ways.

Code

C#

private Button enterBut = new Button();
private void enterButClick(System.Object sender, System.EventArgs e)
{
	Control ctl = (Control)lastObj;
	this.SelectNextControl(ctl, true, true, true, true);
	wasClick = false;
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
	enterBut.Click += enterButClick;
	this.AcceptButton = enterBut;
}
object lastObj = new object();
private void EnterTXT(object sender, System.EventArgs e)
{
	lastObj = sender;
}
bool wasClick;
private void Buttons_Click(System.Object sender, System.EventArgs e)
{
	if (!wasClick)
		enterBut.PerformClick();
}
private void Buttons_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
	wasClick = true;
}

Or

private void TextBox1_KeyDown(System.Object sender, System.Windows.Forms.KeyEventArgs e)
{
	if (e.KeyCode == Keys.Enter) {
		SendKeys.Send("{tab}");
		e.SuppressKeyPress = true;
	}
}

VB .Net

Private WithEvents enterBut As New Button
Private Sub enterButClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim ctl As Control = DirectCast(lastObj, Control)
    Me.SelectNextControl(ctl, True, True, True, True)
    wasClick = False
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    AddHandler enterBut.Click, AddressOf enterButClick
    Me.AcceptButton = enterBut
End Sub
Dim lastObj As New Object
Private Sub EnterTXT(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, Button1.Enter
    lastObj = sender
End Sub
Dim wasClick As Boolean
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Not wasClick Then enterBut.PerformClick()
End Sub
Private Sub Buttons_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
    wasClick = True
End Sub

Or

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown
    If e.KeyCode = Keys.Enter Then
        SendKeys.Send("{tab}")
        e.SuppressKeyPress = True
    End If
End Sub

Screenshot

Lama
 
on Dec 29, 2015 03:55 AM

Thaaaaaaaaaaaaaaanx sooo much, it worked well
Ive been trying sice morning _^