我正在使用C#编写WinForm应用程序,在文本框中输入内容后,我想按Enter键,但文本框仍然保持焦点(闪烁的光标仍在文本框内),我该如何实现此功能?
简单的方法是将表单的AcceptButton设置为您想要按下的按钮(通常是“确定”等):
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
Application.Run(new Form { AcceptButton = btn, Controls = { tb, btn } });
如果这不是一个选项,你可以查看KeyDown事件等,但那需要更多的工作...
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
tb.KeyDown += (sender,args) => {
if (args.KeyCode == Keys.Return)
{
btn.PerformClick();
}
};
Application.Run(new Form { Controls = { tb, btn } });
通常的做法是将Form
的AcceptButton
属性设置为您想要“单击”的按钮。您可以在VS设计器中或在代码中执行此操作,并且可以随时更改AcceptButton
属性。
这可能适用于您的情况,但我已经将其与表单上不同的TextBox
的GotFocus
事件结合使用,以根据用户按下Enter的位置启用不同的行为。例如:
void TextBox1_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox1;
}
void TextBox2_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox2;
}
使用这种方法需要注意的一点是,当TextBox3
获得焦点时,不要将AcceptButton
设置为ProcessTextBox1
。我建议使用在设置AcceptButton
的TextBox
上使用LostFocus
事件,或者创建一个所有控件都调用的GotFocus
方法,而这些控件不使用特定的AcceptButton
。
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button.PerformClick();
// these last two lines will stop the beep sound
e.SuppressKeyPress = true;
e.Handled = true;
}
}
将此KeyDown事件绑定到文本框,然后每当您按键时,此事件将被触发。在事件内部,我们检查用户是否按下了“Enter键”,如果是,则可以执行您的操作。
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox.AcceptsReturn)
{
button1.PerformClick();
}
}
}
在我的情况下,这段代码是自定义UserControl的一部分,该控件派生自TextBox,并且该控件具有“ClickThisButtonOnEnter”属性。但上述是更通用的解决方案。e.Handled
设置为 true
以避免蜂鸣声。 - Ruud只需将表单的“接受按钮”属性设置为您想要按Enter键触发的按钮。
或者在加载事件中编写this.acceptbutton = btnName;
最适合初学者的解决方案是:
In your Designer, click on the text field you want this to happen. At the properties Window (default: bottom-right) click on the thunderbolt (Events). This icon is next to the alphabetical sort icon and the properties icon.
Scroll down to keyDown
. Click on the Dropdown field right to it. You'll notice there's nothing in there so simply press enter. Visual Studio will write you the following code:
private void yourNameOfTextbox_KeyDown(object sender, KeyEventArgs e)
{
}
Then simply paste this between the brackets:
if (e.KeyCode == Keys.Enter)
{
yourNameOfButton.PerformClick();
}
this.btnLogIn = new System.Windows.Forms.Button();
//....other settings
this.AcceptButton = this.btnLogIn;
if (e.KeyCode == Keys.Enter)
button1.PerformClick();
this.textboxName.KeyDown += (sender, args) => {
if (args.KeyCode == Keys.Return)
{
buttonName.PerformClick();
}
};
public class EnterTextBox : TextBox
{
[Browsable(true), EditorBrowsable]
public event EventHandler EnterKeyPressed;
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
EnterKeyPressed?.Invoke(this, EventArgs.Empty);
return true;
}
return base.ProcessDialogKey(keyData);
}
}