Click here to Skip to main content
11,576,354 members (60,836 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET1.0 .NET1.1 .NET2.0 C#2.0 , +
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}"). Smile | :)
  Permalink  
v2
Comments
H.Brydon at 16-Sep-13 23:01pm
   
+5 good answer. :-)
shukla dhaval at 16-Jun-10 5:06am
   
Reason for my vote of 2
this way is proper to achieve your goal.
Nuri Ismail at 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 at 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 at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 251
1 Abhinav S 135
2 Lucifier Rocks 105
3 Dave Kreskowiak 80
4 F-ES Sitecore 80
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 626
2 Abhinav S 483
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 13 Mar 2015
Copyright © CodeProject, 1999-2015
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