C#:如何使在文本框中按 Enter 键触发按钮,但仍允许使用“Ctrl”A 等快捷键要通过?
抱歉,标题很长,但我想不出其他方式来表达它。
我有这个:
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonSubmit_Click((object)sender, (EventArgs)e);
}
}
...为了使在文本框中按 Enter 触发“提交”按钮。然而,这也阻止了快捷方式的通过。 (不太确定它与此有什么关系,也许只是多键组合?)
ShortcutsEnabled 设置为 true。
提前致谢!
Sorry for the long title, but I couldn't think of another way to put it.
I have this:
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonSubmit_Click((object)sender, (EventArgs)e);
}
}
... in order to make pressing enter in the text box trigger the "submit" button. However, this also prevents shortcuts from going through. (not quite sure what it has to do with that, maybe only multi-key combos?)
ShortcutsEnabled is set to true.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能在“表单属性”窗口中使用
AcceptButton
吗?这设置了 Enter 按键的默认行为,但您仍然可以使用其他快捷键。Can you not use
AcceptButton
in for the Forms Properties Window? This sets the default behaviour for the Enter key press, but you are still able to use other shortcuts.如果您希望返回仅在用户位于文本框中时触发操作,则可以将所需的按钮分配给 AcceptButton 控件,如下所示。
If you want the return to trigger an action only when the user is in the textbox, you can assign the desired button the AcceptButton control, like this.
您可以使用 KeyPress 而不是 KeyUp 或 KeyDown 它更有效
这是如何处理
希望它有效的方法
You can Use KeyPress instead of KeyUp or KeyDown its more efficient
and here's how to handle
hope it works
如果是 ASP.NET,则不需要任何客户端代码。
下面的示例是一个带有搜索按钮和 fontawesome 图标的 boostrap 输入框。
您将看到它代替了常规的 <分区>带有“输入组”类的标签我使用了 asp:Panel。将 DefaultButton 属性设置为我的按钮的 id 就可以了。
在下面的示例中,在输入文本框中输入内容后,只需按 Enter 键即可提交。
You do not need any client side code if doing this is ASP.NET.
The example below is a boostrap input box with a search button with an fontawesome icon.
You will see that in place of using a regular < div > tag with a class of "input-group" I have used a asp:Panel. The DefaultButton property set to the id of my button, does the trick.
In example below, after typing something in the input textbox, you just hit enter and that will result in a submit.
https://stackoverflow.com/a/16350929/11860907
如果添加
e.SuppressKeyPress = true;< /code> 如此链接中的答案所示,您将抑制出现的烦人的叮当声。
https://stackoverflow.com/a/16350929/11860907
If you add
e.SuppressKeyPress = true;
as shown in the answer in this link you will suppress the annoying ding sound that occurs.