在文本框中按Enter键并执行按钮命令 [英] Press enter in textbox to and execute button command

查看:144
本文介绍了在文本框中按Enter键并执行按钮命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过按 Enter 来执行搜索"按钮后面的代码.我的搜索按钮具有接受按钮"属性.但是,当我将按钮设置为不可见时,我的搜索不会执行.

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 并在按钮不可见时执行我的按钮.任何建议将是巨大的!以下是我在KeyDown事件中的代码的一次尝试

I want to be able to press Enter within 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

if (e.KeyCode == Keys.Enter)
    {
        buttonSearch_Click((object)sender, (EventArgs)e);
    }

推荐答案

您可以注册到文本框的KeyDown-Event,查看所按下的键是否为 Enter ,然后执行按钮:

You could register to the KeyDown-Event of the Textbox, look if the pressed key is Enter and then execute the EventHandler of the button:

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());
    }
}

这篇关于在文本框中按Enter键并执行按钮命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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