【问题标题】:C# Textbox validation should only accept integer values, but allows letters as wellC# 文本框验证应该只接受整数值,但也允许字母
【发布时间】:2010-04-02 22:46:25
【问题描述】:
if (textBox1.Text != "")  // this forces user to enter something
{
  // next line is supposed to allow only 0-9 to be entered but should block all...
  // ...characters and should block a backspace and a decimal point from being entered....
  // ...but it is also allowing characters to be typed in textBox1
  if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46)  // 46 is a "."
  {  
     e.Handled=true;
  }
  else 
  {
     e.Handled=false;
  }  

  if (KeyCode == 13) // enter key
  {  
    TBI1 = System.Convert.ToInt32(var1);   // converts to an int
    Console.WriteLine("TBI1 (var1 INT)= {0}", var1);
    Console.WriteLine("TBI1= {0}", TBI1);
  } 

  if (KeyCode == 46)
  {
    MessageBox.Show("Only digits...no dots please!"); 
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); 
  }
}
else
{
   Console.WriteLine("Cannot be empty!");
}

// If I remove the outer if statement and skip checking for an empty string, then
// it prevents letters from being entered in the textbox. I need to do both, prevent an 
// empty textbox AND prevent letters from being entered.
// thanks, Sonny5

【问题讨论】:

    标签: c# validation textbox integer console-application


    【解决方案1】:

    您没有指定此代码的运行位置,但我的假设是它在按键下运行。由于在处理字符和更新 Text 属性之前收到 key down,因此您对 .Text == "" 的检查将阻止其余的验证运行,至少对于第一个字符。

    您应该将空值检查移到与按下的键检查不同的事件上。

    【讨论】:

    • 如果我还有一票,你会得到 +1。代码中的注释“这会强制用户输入某些内容”是错误的。那根本不是那条线的作用。
    【解决方案2】:

    我认为你可以使用IsDigit 函数。

    类似的东西:

    string textBoxText = "12kj3";
    
    if (!textBoxText.Equals(String.Empty))  // this forces user to enter something
    {
        foreach (char c in textBoxText.ToArray())
        {
            if (!Char.IsDigit(c))
            {
                //return false;
            }
        }
    
        //return true;
    }
    else
    {
        Console.WriteLine("Cannot be empty!");
    }
    

    希望你能明白。

    【讨论】:

      【解决方案3】:

      您可以使用以下RegEx来检查它是否是一个数字“^\d+$”并且是必需的。

      【讨论】:

        【解决方案4】:
        bool bV=false;
            private void textBox1_Validated(object sender, EventArgs e)
            {
                TextBox textBoxText = sender as TextBox;
        
                if (!textBoxText.Equals(String.Empty))  
                {
                    foreach (char c in textBoxText.Text.ToArray())
                    {
                        if (!Char.IsDigit(c))
                        {
                            if (!bV)
                            {
                                MessageBox.Show("Input value not valid plase Insert Integer Value");
                                bV = true;
                                textBox1.Text = String.Empty;
                                break;
                            }
                        }
                    }
                }
            }
        
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                bV = false;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-24
          • 1970-01-01
          • 1970-01-01
          • 2020-05-11
          • 2023-02-24
          • 2016-10-07
          • 1970-01-01
          • 2021-06-21
          相关资源
          最近更新 更多