【问题标题】:Restrict numbers and letters in textbox - C#限制文本框中的数字和字母 - C#
【发布时间】:2012-03-07 12:58:46
【问题描述】:

我想限制可以在文本框中输入的数字和字母。假设我只想允许数字 0-5 和字母 a-d(小写和大写)。 我已经尝试过使用蒙面文本框,但它只能让我指定数字、字母(都没有限制)或数字和字母一起但按特定顺序。 最好的情况是:用户尝试输入数字 6,但文本框中没有输入任何内容,对于 a-f 范围之外的字母也是如此。 我认为最好使用的事件是 Keypress 事件,但我不知道如何实现限制。

【问题讨论】:

  • 别忘了复制/粘贴...
  • 参见this question which will probably help you(适用于 ASP.NET)
  • winforms、sl、wpf、wp7、asp.net webforms 还是 mvc?
  • 这和WinForms有关,抱歉我忘记了这个细节

标签: c# winforms


【解决方案1】:

为您的文本框使用 KeyPress 事件。

protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs)
{
    e.Handled = !IsValidCharacter(e.KeyChar);
}

private bool IsValidCharacter(char c)
{
    bool isValid = true;

    // put your logic here to define which characters are valid
    return isValid;
}

【讨论】:

    【解决方案2】:
    // Boolean flag used to determine when a character other than a number is entered.
    private bool nonNumberEntered = false;
    
    // Handle the KeyDown event to determine the type of character entered into the control.
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        // Initialize the flag to false.
        nonNumberEntered = false;
    
        // Determine whether the keystroke is a number from the top of the keyboard.
        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            // Determine whether the keystroke is a number from the keypad.
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                // Determine whether the keystroke is a backspace.
                if(e.KeyCode != Keys.Back)
                {
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
                }
            }
        }
        //If shift key was pressed, it's not a number.
        if (Control.ModifierKeys == Keys.Shift) {
            nonNumberEntered = true;
        }
    }
    
    // This event occurs after the KeyDown event and can be used to prevent
    // characters from entering the control.
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        // Check for the flag being set in the KeyDown event.
        if (nonNumberEntered == true)
        {
            // Stop the character from being entered into the control since it is non-numerical.
            e.Handled = true;
        }
    }
    

    【讨论】:

    • 这就像一个魅力!我只需要添加字母的限制。现在我想到了,如果我有 3 个格式相同的文本框并且我想检查所有这些文本框,那么做同样事情的最佳方法是什么?我正在考虑将所有内容都放入一个方法中,但后来我意识到有些事件属于特定控件。我可以为每个文本框复制相同的代码,但这将是很多代码!你会怎么做?
    【解决方案3】:

    像这样覆盖 PreviewKeyDownEvent:

        private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.A || e.KeyCode == Keys.B || ...)
                e.IsInputKey = true;
            else
                e.IsInputKey = false;
        }
    

    这将告诉文本框哪些键将被视为用户输入。

    【讨论】:

      【解决方案4】:

      使用 KeyDown 事件,如果 e.Key 不在您的允许集中,则只需 e.Handled = true

      另一种方法是接受所有输入,对其进行验证,然后向用户提供有用的反馈,例如要求他们输入特定范围内的数据的错误标签。我更喜欢这种方法,因为用户知道出了问题并且可以修复它。它在整个 Web 表单中使用,对于您的应用程序的用户来说一点也不奇怪。按下一个键却完全没有反应可能会令人困惑!

      http://en.wikipedia.org/wiki/Principle_of_least_astonishment

      【讨论】:

      • 这与 WinForms 有关,抱歉我忘记了。
      • 没问题 - 我在您的问题中添加了该标签。好的 UI 设计原则仍然适用!
      【解决方案5】:

      Keypress 事件可能是您最好的选择。在那里检查输入的字符是否不是您想要的字符,将e.SuppressKey 设置为true 以确保不会触发 KeyPress 事件,并且不会将字符添加到文本框中。

      【讨论】:

        【解决方案6】:

        如果您使用的是 ASP.NET Web 窗体,则正则表达式验证将是最简单的。在 MVC 中,一个 jQuery 库(例如 MaskedEdit)将是一个很好的起点。上面的答案很好地证明了 Windows 窗体方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-12
          • 1970-01-01
          • 2021-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多