C# 在文本框中按 Enter 并执行按钮命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19975617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Press enter in textbox to and execute button command
提问by hex c
I want to execute the code behind my Search Button by pressing Enter. I have the Accept Button property to my search button. However, when i place my button as NOT visible my search doesn't execute.
我想通过按 来执行我的搜索按钮后面的代码Enter。我的搜索按钮有 Accept Button 属性。但是,当我将按钮放置为不可见时,我的搜索不会执行。
I want to be able to press Enterwithin my text box and execute my button while its not visible. Any suggestions would be great! Below is one attempt at my code in KeyDown Event
我希望能够Enter在我的文本框中按下并在它不可见时执行我的按钮。任何建议都会很棒!下面是我在 KeyDown 事件中的代码的一次尝试
if (e.KeyCode == Keys.Enter)
{
buttonSearch_Click((object)sender, (EventArgs)e);
}
采纳答案by Philipp Eger
You could register to the KeyDown-Event of the Textbox, look if the pressed key is Enterand then execute the EventHandler of the button:
您可以注册到Textbox的KeyDown-Event,查看按下的键是否被按下Enter,然后执行按钮的EventHandler:
private void buttonTest_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
private void textBoxTest_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonTest_Click(this, new EventArgs());
}
}
回答by Sudhakar Tillapudi
You can handle the keydown event of your TextBox
control.
您可以处理TextBox
控件的 keydown 事件。
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
buttonSearch_Click(sender,e);
}
It works even when the button Visible
property is set to false
即使将按钮Visible
属性设置为false
回答by Noctis
Since everybody covered the KeyDown
answers, how about using the IsDefault
on the button?
既然每个人都涵盖了KeyDown
答案,那么使用IsDefault
按钮上的怎么样?
You can read this tip for a quick howto and what it does: http://www.codeproject.com/Tips/665886/Button-Tip-IsDefault-IsCancel-and-other-usability
您可以阅读此提示以获取快速操作方法及其功能:http: //www.codeproject.com/Tips/665886/Button-Tip-IsDefault-IsCancel-and-other-usability
回答by Carol
Alternatively, you could set the .AcceptButton property of your form. Enter will automcatically create a click event.
或者,您可以设置表单的 .AcceptButton 属性。Enter 会自动创建一个点击事件。
this.AcceptButton = this.buttonSearch;
回答by Keramat
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//cod for run
}
}
private void buttonSearch_Click(object sender, EventArgs e)
{
textbox1_KeyDown(sender, new KeyEventArgs(Keys.Enter));
}
回答by keysl
If you're just gonna click the button when Enter was pressed how about this?
如果您只是在按下 Enter 键时单击按钮,那该怎么办?
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonSearch.PerformClick();
}
回答by sreejith dinesan
In WPF
apps This code working perfectly
在WPF
应用程序中此代码完美运行
private void txt1_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter) )
{
Button_Click(this, new RoutedEventArgs());
}
}
回答by Ricardo
there you go.
你去吧。
private void YurTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
YourButton_Click(this, new EventArgs());
}
}
回答by volody
If buttonSearch has no code, and only action is to return dialog result then:
如果 buttonSearch 没有代码,并且唯一的操作是返回对话框结果,则:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
DialogResult = DialogResult.OK;
}
回答by titol
There are some cases, when textbox will not handle enter key. I think it may be when you have accept button set on form. In that case, instead of KeyDown
event you should use textbox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
在某些情况下,文本框不会处理回车键。我认为可能是当您在表单上设置了接受按钮时。在这种情况下,KeyDown
您应该使用事件而不是事件textbox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)