Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET1.0 .NET1.1 .NET2.0 C#1.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
jpratik1.8K
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

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 509
1 Nirav Prabtani 338
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 Sep 2013
Copyright © CodeProject, 1999-2014
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