Click here to Skip to main content
12,954,522 members (63,595 online)
Rate this:
 
Please Sign up or sign in to vote.
How to Enable Enter Button To Move To Next Control Instead to Tab using C# in Windows Forms for a Textbox ?
Posted 15-Jun-10 21:51pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
     }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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}"). :)
  Permalink  
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. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Select form in ide and enable key preview to true.
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

 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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Below is perfect code to move next control in windows forms using 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;
            }
        }
}
  Permalink  
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
anjankant 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,143
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 13 Mar 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100