[英]Press Enter to move to next control
I have a few TextBox on the WinForm.我在 WinForm 上有几个 TextBox。 I would like the focus to move to the next control when Enter key is pressed?
当按下 Enter 键时,我希望焦点移动到下一个控件? 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. Tab as Enter:创建一个继承文本框的用户控件,覆盖
KeyPress
方法。 If the user presses enter you can either call SendKeys.Send("{TAB}")
or System.Windows.Forms.Control.SelectNextControl()
.如果用户按 Enter,您可以调用
SendKeys.Send("{TAB}")
或System.Windows.Forms.Control.SelectNextControl()
。 Note you can achieve the same using the KeyPress
event.请注意,您可以使用
KeyPress
事件实现相同的效果。
Focus Entire text: Again, via override or events, target the GotFocus
event and then call TextBox.Select
method.聚焦整个文本:同样,通过覆盖或事件,以
GotFocus
事件为目标,然后调用TextBox.Select
方法。
A couple of code examples in C# using SelectNextControl .在 C# 中使用SelectNextControl 的几个代码示例。
The first moves to the next control when ENTER is pressed.当按下ENTER时,第一个移动到下一个控件。
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参见 MSDN SelectNextControl 方法
In a KeyPress event, if the user pressed Enter, call在 KeyPress 事件中,如果用户按下 Enter,则调用
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:在接收焦点时实现自动选择文本的最好方法是使用以下覆盖在您的项目中创建 TextBox 的子类:
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.然后在所有表单上使用此自定义 TextBox 代替 WinForms 标准 TextBox。
You can put a KeyPress
handler on your TextBoxes, and see which key was used.您可以在
KeyPress
上放置KeyPress
处理程序,并查看使用了哪个键。
To handle the text selection, put a handler on the GotFocus
event.要处理文本选择,请在
GotFocus
事件上放置一个处理程序。
You may also want to consider how to (or if you need to) handle multi-line TextBoxes.您可能还需要考虑如何(或如果需要)处理多行文本框。
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!");
}
}
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();
}
} }
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;
}
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SendKeys.Send("{TAB}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
goto the design form and View-> tab(as like picture shows) Order then you ordered all the control[That's it]转到设计表单和视图-> 选项卡(如图所示)订购然后您订购了所有控件[就是这样]
尝试使用:
SendKeys.Send("{TAB}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.