Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
How to Enable Enter Button To Move To Next Control Instead to Tab using C# in Windows Forms for a Textbox ?
Posted

C#
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == '\r')
     {
	if (this.ActiveControl != null)
        {
            this.SelectNextControl(this.ActiveControl, true, true, true, true);
        }
	e.Handled = true; // Mark the event as handled
     }
}
 
Share this answer
 
One possible way is to handle the KeyPress[^] event, check for ENTER key and set the focus to the next control.
But for your case I'd prefer creating a user control which inherits TextBox and overrides the KeyPress method.
If the user presses ENTER you can call Control.SelectNextControl() or SendKeys.Send("{TAB}"). :)
 
Share this answer
 
v2
Comments
H.Brydon 16-Sep-13 23:01pm    
+5 good answer. :-)
shukla dhaval 16-Jun-10 5:06am    
Reason for my vote of 2
this way is proper to achieve your goal.
Nuri Ismail 16-Jun-10 5:08am    
If you think that it is proper why you give me a 2? You know that 1 and 2 are down-votes and 4 and 5 are up-votes, right? :)
jpratik 16-Jun-10 5:49am    
Thanks for your answer.
Once implement i am getting the required result but, a beep of sound is present.Is there any way to avoid it ?
I do not know why its coming but i want to avoid it.
Nuri Ismail 16-Jun-10 5:59am    
Probably you miss to mark the KeyPress event as handled. In your KeyPress event handler you should have a KeyPressEventArgs e as a second parameter. Set the Handled property of this parameter to true when you handle the ENTER key press.

Example:
void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
SelectNextControl();
e.Handled = true; // Mark the event as handled
}
}

I hope this helps. :)
Select form in ide and enable key preview to true.
VB
Private Sub myEventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      If e.KeyCode = Keys.Enter Then

          e.SuppressKeyPress = True
          Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
      End If
  End Sub

VB
 Private Sub addcustomer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ctl As Control
        For Each ctl In Me.Controls
            AddHandler ctl.KeyDown, AddressOf myEventHandler
        Next 
End Sub
 
Share this answer
 
v2
Below is perfect code to move next control in windows forms using c#

C#
private void txtLoginID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                this.SelectNextControl(this.ActiveControl, true, true, true, true);
                e.Handled = true;
            }
        }
}
 
Share this answer
 
v2
Comments
CHill60 13-Mar-15 9:51am    
The question was asked over 4 years ago and is already adequately answered. This adds nothing to the information provided by the earlier solutions
[no name] 18-Mar-15 1:37am    
OK!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900