如何在文本框中输入单击按钮

编程入门 行业动态 更新时间:2024-10-24 16:24:28
本文介绍了如何在文本框中输入单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的, 我在对话框上有一个文本框和一个按钮. 我想要以下功能: 如果我在文本框中按Enter,则单击确定按钮. 如果按ESC,则单击取消按钮. 我想传递与按钮单击事件传递的事件参数和对象相同的事件 请帮助我. 在此先感谢

Hi Dears, I have a textbox and a button on a dialog. What I want following function: if i press Enter on the textbox, the button Ok is clicked if I press ESC ,the button cancel is clicked. I want to pass the same event arguments and objects as passed by button click event Please help me on this. Thanks in advance

推荐答案

一个简单的Google搜索将为您提供所需的内容.如果您用Google搜索"C#文本框按钮,请单击Enter",那么您应该能够找到该信息. 您所要做的就是处理TextBox的KeyDown事件. Button也有一个称为PerformClick()的方法.在KeyDown中,只需检查Enter或转义 A simple google search would provide you with what you need. If you had googled "C# textbox button click on enter" you should have been able to find the information. All you have to do is handle the KeyDown event of the TextBox. The Button has a method called PerformClick() as well. In the KeyDown, just check for Enter or escape private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { cmdCancel.PerformClick(); } else if (e.KeyCode == Keys.Enter) { cmdOk.PerformClick(); } }

就是这么简单.

It''s that simple.

您可以将 AcceptButton 属性设置为确定按钮(btnOkButton) 和 CancelButton 设置为设计时的取消按钮 谢谢 :) you can set AcceptButton Property of form to you ok button (btnOkButton) and CancelButton to your cancel button at design time Thanks :)

J imran写道: J imran wrote:

我有一个文本框和一个按钮

I have a textbox and a button

好的 您的要求是

ok Your requirement is

J imran写道: J imran wrote:

如果我在文本框中按Enter,则单击确定按钮. 如果按ESC键,则单击取消按钮.

if i press Enter on the textbox, the button Ok is clicked if I press ESC ,the button cancel is clicked.

因此,在那个伊朗发生的重大事情并不难做到 她是您的烘焙代码,但让我向您解释我做了什么, 我已经生成了两个按钮的click事件,并编写了显示消息框的代码,您可以根据需要修改代码

so whats the big deal in that imran it was not that hard to do so hers your baked code but let me explain u what i have done, I have generated click event of both the buttons and have wrote code to display messageboxes u may modify the code as per your need

private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Button Ok is clicked"); } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button cancel is clicked"); } private void textBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { button2_Click(sender, e); } if (e.KeyCode == Keys.Enter) { button1_Click(sender, e); } }

一旦找到有用的答案,就对我的答案进行评分 谢谢&问候 基:rose:

Do rate my answer once you find it useful Thanks & Regards Radix :rose:

更多推荐

如何在文本框中输入单击按钮

本文发布于:2023-05-25 01:47:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   文本   框中输入   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!