【问题标题】:C# Numeric Only TextBox Control [duplicate]C#仅数字文本框控件[重复]
【发布时间】:2011-02-17 12:03:28
【问题描述】:

我使用的是 C#.NET 3.5,但我的项目有问题。在 C# Windows 应用程序中,我想创建一个 textbox 来只接受数字。如果用户尝试输入字符,消息应该显示为“请仅输入数字”,并且在另一个文本框中它必须接受有效的email id 消息,当它无效时应该出现。它必须显示无效的用户 ID。

【问题讨论】:

    标签: c# textbox .net-3.5


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      从 C#3.5 开始,我假设您使用的是 WPF。

      只需从整数属性到文本框进行双向数据绑定。 WPF 会自动为您显示验证错误。

      对于电子邮件案例,从一个字符串属性进行双向数据绑定,该属性在 setter 中进行正则表达式验证,并在验证错误时抛出异常。

      在 MSDN 上查找 Binding。

      【讨论】:

        【解决方案3】:

        使用此代码:

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
                {
                    const char Delete = (char)8;
                    e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
                }
        

        【讨论】:

        • 这不会阻止用户将任何文本粘贴到文本框中。
        【解决方案4】:

        您可能想在KeyPress(object, KeyPressEventArgs) 事件中尝试int.TryParse(string, out int) 来检查数值。对于其他问题,您可以改用正则表达式。

        【讨论】:

          【解决方案5】:

          我使用了 @fjdumont 提到的 TryParse,但在验证事件中改为使用。

          private void Number_Validating(object sender, CancelEventArgs e) {
              int val;
              TextBox tb = sender as TextBox;
              if (!int.TryParse(tb.Text, out val)) {
                  MessageBox.Show(tb.Tag +  " must be numeric.");
                  tb.Undo();
                  e.Cancel = true;
              }
          }
          

          我将它附加到两个不同的文本框中,并在我的表单中初始化代码。

              public Form1() {
                  InitializeComponent();
                  textBox1.Validating+=new CancelEventHandler(Number_Validating);
                  textBox2.Validating+=new CancelEventHandler(Number_Validating);
              }
          

          我还添加了tb.Undo() 以取消无效更改。

          【讨论】:

            【解决方案6】:

            这种方式适合我:

            private void textboxNumberic_KeyPress(object sender, KeyPressEventArgs e)
            {
                 const char Delete = (char)8;
                 e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
            }
            

            【讨论】:

              【解决方案7】:

              试试这个代码

              // 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, 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;
                          }
                      }
                  }
              }
              
              private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
              {
                  if (nonNumberEntered == true)
                  {
                     MessageBox.Show("Please enter number only..."); 
                     e.Handled = true;
                  }
              }
              


              来源是http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.90).aspx

              【讨论】:

              • 光标键和删除键呢?如果 OP 使用 Windows 窗体,他可以使用 Daniel 的解决方案。
              • 一个与此相关的错误:尝试按“SHIFT + 1”看看会发生什么。
              【解决方案8】:

              您可以通过 e.keychar 在 TextBox 的 KeyPress 事件上检查 Ascii 值。

              通过检查 AscII 值,您可以检查数字或字符。

              同样,您可以编写逻辑来检查电子邮件 ID。

              【讨论】:

              • 如果我是邪恶的测试者,我会粘贴一些文字...
              【解决方案9】:

              我觉得对你有帮助

              <script type="text/javascript">
              function isNumberKey(evt) {
                  var charCode = (evt.which) ? evt.which : event.keyCode
                  if (charCode > 32 && (charCode < 48 || charCode > 57) && (charCode != 45) && (charCode != 43) && (charCode != 40) && (charCode != 41))
                      return false;
              
                  return true;
              }
              

              【讨论】:

                【解决方案10】:
                try
                {
                    int temp=Convert.ToInt32(TextBox1.Text);
                }
                catch(Exception h)
                {
                    MessageBox.Show("Please provide number only");
                }
                

                【讨论】:

                • 你想给操作什么代码?没有意义
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-07-02
                • 1970-01-01
                • 2021-05-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多