如何在 C# 中跳过文本框的输入?

发布于 2024-12-27 18:39:48 字数 363 浏览 3 评论 0原文

我想从 C# 的 TextBox 组件扩展一个类并重写“On[Event]”方法,以便检查在文本框中键入的输入并跳过要在文本框中输入的一些键。我正在寻找这样的东西(以下代码不是解决方案!):

    public class NumText : TextBox
    {
         protected override void OnKeyDown(KeyEventArgs e)
         {
              if (e.KeyValue >= 48 && e.KeyValue <= 57)
                  base.OnKeyDown(e);
         }
    }

I want to extend a class from TextBox component of C# and override an "On[Event]" method in order to check the input of typing in text box and skip some keys to be entered in the text box. I'm search for something like this (the following code is not the solution!):

    public class NumText : TextBox
    {
         protected override void OnKeyDown(KeyEventArgs e)
         {
              if (e.KeyValue >= 48 && e.KeyValue <= 57)
                  base.OnKeyDown(e);
         }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2025-01-03 18:39:48

好吧,只是不要覆盖事件,如果您需要防止按下多个键,请添加一个新事件。

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this_onKeyPress);

private void this_onKeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyValue >= 48 && e.KeyValue <= 57)
        e.Handled = true;
}

Well just don't override the Event ,add a new Event instead if you need to prevent several Key's being pressed .

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this_onKeyPress);

private void this_onKeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyValue >= 48 && e.KeyValue <= 57)
        e.Handled = true;
}
习ぎ惯性依靠 2025-01-03 18:39:48

使用 KeyEventArgs.Handled 属性。

引用MSDN文章:

true 绕过控件的默认处理;否则,为假
还将事件传递给默认控制处理程序。

Use KeyEventArgs.Handled Property.

Quote to MSDN article :

true to bypass the control's default handling; otherwise, false to
also pass the event along to the default control handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文