我正在制作一个程序,我正在使用这段代码,
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
txtbox.Text = txtbox.Text + e.KeyChar.ToString();
}
}
但我的文本框上没有按键。我无法找到失败原因。
最佳答案
根据 this你必须设置 Form.KeyPreview
为 true
,默认为 false。否则您无法处理表单的 KeyPress
事件。
When this property is set to
true
, the form will receive allKeyPress
,KeyDown
, andKeyUp
events.
关于c# - 如何处理窗体的 KeyPress 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193903/