如何在文本框中输入单击按钮 [英] how to enter on a textbox to click a button

查看:112
本文介绍了如何在文本框中输入单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,
我在对话框上有一个文本框和一个按钮.
我想要以下功能:
如果我在文本框中按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:


这篇关于如何在文本框中输入单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆