问题描述
我有一个按钮,形式为
当用户按下空格事件的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
推荐答案
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.
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[^]
这篇关于如何在C#中以表格形式设置按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!