我有这样的要求,即用户需要使用键盘数字小键盘上的键来控制分配给它的特定按钮并执行每个功能。
例子:
如果 Numpad 键 0 被按下,那么 Button0 将被触发。
或者
if(Numpad0 is pressed)
{
//do stuff
if (inputStatus)
{
txtInput.Text += btn0.Text;
}
else
{
txtInput.Text = btn0.Text;
inputStatus = true;
}
}
else if(Numpad1 is pressed)
{
//do stuff
}
在我的表单中,我有一个拆分容器,然后所有按钮都位于一个组合框中。
最佳答案
将KeyPreview
设置为true
并处理KeyDown
:
private void Form_KeyDown(object sender, KeyDownEventArgs e) {
if(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
((Button) this["Button" + (e.KeyCode - Keys.NumPad0).ToString()]).PerformClick();
}
我还没有测试过,但这就是我将如何做的。
关于c# - 如何在 Winform 中通过 Numpad 键控制按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500663/