C# 文本框验证应该只接受整数值,但也允许字母

发布于 2024-08-27 04:44:31 字数 1174 浏览 3 评论 0原文

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
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

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

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

发布评论

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

评论(4

你不是我要的菜∠ 2024-09-03 04:44:31

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

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

You didn't specify where this code runs, but my assumption would be it runs on key down. Since key down is received before the character is processed and the Text property is updated, your check for .Text == "" will prevent the rest of the validation running, at least for the first character.

You should move the check for empty value on a different event than the check for the key pressed.

夜司空 2024-09-03 04:44:31

我认为您可以使用 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!");
}

希望你明白了。

I think you could use the IsDigit function.

Something along these lines:

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!");
}

Hope you get the idea.

无人问我粥可暖 2024-09-03 04:44:31

您可以使用以下正则表达式来检查它是否是数字“^\d+$”并且是必需的。

You can use the following RegEx to check that it is a number "^\d+$" and required.

寄意 2024-09-03 04:44:31
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;
    }
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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文