c# - 如何处理窗体的 KeyPress 事件?

标签 c# .net winforms

我正在制作一个程序,我正在使用这段代码,

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{          
    if (e.KeyChar >= 48 && e.KeyChar <= 57)
    {
        txtbox.Text = txtbox.Text + e.KeyChar.ToString();
    }
}

但我的文本框上没有按键。我无法找到失败原因。

最佳答案

根据 this你必须设置 Form.KeyPreviewtrue,默认为 false。否则您无法处理表单的 KeyPress 事件。

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events.

关于c# - 如何处理窗体的 KeyPress 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193903/

相关文章:

c# - .Net Application Loader 的命令行解析器

c# - 在 Windows 窗体上的较长过程中显示动画

c# - 使winforms在没有UI的情况下运行的命令行

c# - 如何使用WindowsAzure.Storage.DataMovement?

c# - 处理 UWP 应用程序中的 alt 键

c# - 如何使 DataGridView 显示选定的行?

c# - EmguCV/OpenCV QueryFrame 缓慢/缓冲

c# - MEF 组合无法找到 System.Data.Edm 5.6

c# - Windows 窗体启动画面 - 在加载主窗体时显示窗体

c# - .NET 单击一次部署 App.config