I have a few TextBox on the WinForm. I would like the focus to move to the next control when Enter key is pressed? Whenever a textbox gains control, it will also select the text, so that any editing will replace the current one.
What is the best way to do this?
Tab as Enter: create a user control which inherits textbox, override the KeyPress
method. If the user presses enter you can either call SendKeys.Send("{TAB}")
or System.Windows.Forms.Control.SelectNextControl()
. Note you can achieve the same using the KeyPress
event.
Focus Entire text: Again, via override or events, target the GotFocus
event and then call TextBox.Select
method.
In a KeyPress event, if the user pressed Enter, call
SendKeys.Send("{TAB}")
Nicest way to implement automatically selecting the text on receiving focus is to create a subclass of TextBox in your project with the following override:
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
SelectionStart = 0
SelectionLength = Text.Length
MyBase.OnGotFocus(e)
End Sub
Then use this custom TextBox in place of the WinForms standard TextBox on all your Forms.
You could also write your own Control for this, in case you want to use this more often. Assuming you have multiple TextBoxes in a Grid, it would look something like this:
public class AdvanceOnEnterTextBox : UserControl
{
TextBox _TextBox;
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(AdvanceOnEnterTextBox), null);
public static readonly DependencyProperty InputScopeProperty = DependencyProperty.Register("InputScope", typeof(InputScope), typeof(AdvanceOnEnterTextBox), null);
public AdvanceOnEnterTextBox()
{
_TextBox = new TextBox();
_TextBox.KeyDown += customKeyDown;
Content = _TextBox;
}
/// <summary>
/// Text for the TextBox
/// </summary>
public String Text
{
get { return _TextBox.Text; }
set { _TextBox.Text = value; }
}
/// <summary>
/// Inputscope for the Custom Textbox
/// </summary>
public InputScope InputScope
{
get { return _TextBox.InputScope; }
set { _TextBox.InputScope = value; }
}
void customKeyDown(object sender, KeyEventArgs e)
{
if (!e.Key.Equals(Key.Enter)) return;
var element = ((TextBox)sender).Parent as AdvanceOnEnterTextBox;
if (element != null)
{
int currentElementPosition = ((Grid)element.Parent).Children.IndexOf(element);
try
{
// Jump to the next AdvanceOnEnterTextBox (assuming, that Labels are inbetween).
((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition + 2)).Focus();
}
catch (Exception)
{
// Close Keypad if this was the last AdvanceOnEnterTextBox
((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = false;
((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = true;
}
}
}
}
You can put a KeyPress
handler on your TextBoxes, and see which key was used.
To handle the text selection, put a handler on the GotFocus
event.
You may also want to consider how to (or if you need to) handle multi-line TextBoxes.
private void txt_invoice_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
txt_date.Focus();
}
private void txt_date_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
txt_patientname.Focus();
}
}
This may help:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//
// Detect the KeyEventArg's key enumerated constant.
//
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You pressed enter! Good job!");
}
}
Try to use:
SendKeys.Send("{TAB}")
A couple of code examples in C# using SelectNextControl.
The first moves to the next control when ENTER is pressed.
private void Control_KeyUp( object sender, KeyEventArgs e )
{
if( (e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return) )
{
this.SelectNextControl( (Control)sender, true, true, true, true );
}
}
The second uses the UP and DOWN arrows to move through the controls.
private void Control_KeyUp( object sender, KeyEventArgs e )
{
if( e.KeyCode == Keys.Up )
{
this.SelectNextControl( (Control)sender, false, true, true, true );
}
else if( e.KeyCode == Keys.Down )
{
this.SelectNextControl( (Control)sender, true, true, true, true );
}
}
See MSDN SelectNextControl Method
Source: Stackoverflow.com