如何在C#中以表格形式设置按键 [英] how set keypress in form in c#

查看:173
本文介绍了如何在C#中以表格形式设置按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,形式为
当用户按下空格事件的button_click正在运行时,我想要
怎么做???
tnx

I have a button in form
I want when user press space event''s button_click is running
how do this????
tnx

推荐答案

执行以下操作:

将表单的KeyPreview设置为true,以便表单可以捕获空格键.

Do the following:

Set the form''s KeyPreview to true so that the form can capture the Spacebar.

this.KeyPreview = true;


然后使用以下形式的KeyDown事件:


Then use the KeyDown event of the form:

this.KeyDown += new KeyEventHandler(Form1_KeyDown);



假设您有一个按钮的事件处理程序,如下所示:



Suppose you have a button''s event handler like this:

private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button clicked");
        }



在Form1_KeyDown函数中使用button1.PerformClick():



Use the button1.PerformClick() in the Form1_KeyDown function:

void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                button1.PerformClick();  // This will call the button1_Click event handler
            }
        }



现在应该可以工作.



Should work now.


早上好,

设置
Good morning,

Set
this.KeyPreview = true;


在表单的构造函数上,或者只是使用表单设计器并将KeyPreview参数设置为true.

然后,您可以使用KeyPress,KeyDown或KeyUp事件来处理表单上的键盘输入.

看看:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx [ ^ ]


on your form''s constructor, or simply use the form designer and set the KeyPreview parameter to true.

Then you can use the KeyPress, KeyDown or KeyUp events to handle keyboard input on the form.

Take a look at:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^]


您必须处理以下关键事件之一:keydown,keyup和keypress.然后,让您的click事件调用一个方法,如果按下空格,则调用相同的方法.
You have to handle one of the key events, keydown, keyup, keypress. Then, have your click event call a method and call the same method if space is pushed.


这篇关于如何在C#中以表格形式设置按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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