【问题标题】:Textbox cannot empty or enter digit 0 c#文本框不能为空或输入数字 0 c#
【发布时间】:2016-06-07 11:25:38
【问题描述】:

我使用这个代码:

 private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 13)
        {
            if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Length == 0) 
            {
                MessageBox.Show("Textbox Cannot Empty or digit 0");
                textBox5.Focus();
            }
            else
            {
                MessageBox.Show("Success!");
            }
            e.Handled = true;
        }
    }

当我清空文本框时,会出现我期望的消息框。但是当我输入数字/数字“0”时出现消息框成功?为了验证,我使用了数字。为了验证,我只想使用数字 1-9。有人可以帮帮我吗?

【问题讨论】:

  • 旁注:|| textBox5.Text.Length == 0 是多余的,因为 IsNullOrWhiteSpace 已经包含它
  • 0比较时,您不想检查文本框的长度,而是要检查实际的text - @ 987654325@(或解析为 int 并检查 0 而不是 "0"
  • 你在哪里应用验证?
  • @petelids 这种情况将如何运作textBox5.Text.Length == "0"。长度是一个整数
  • @PawanNogariya 他不使用长度属性来检查实际文本

标签: c# .net winforms visual-studio validation


【解决方案1】:

如果你想验证数字并且只允许 1 到 9 之间的整数,你应该使用int.TryParse:

if (e.KeyChar == 13)
{
    int number;
    if(int.TryParse(textBox5.Text, out number) && number >= 1 && number <= 9)
    {
        MessageBox.Show("Success!");
    }
    else
    {
        MessageBox.Show("Textbox must contain an integer between 1 and 9");
        textBox5.Focus();
    }
    e.Handled = true;
}

旁注:|| textBox5.Text.Length == 0 是多余的,因为string.IsNullOrWhiteSpace(textBox5.Text) 已经检查过了。

【讨论】:

  • 谢谢你的帮助,我试过了。但我一直在得到@Maximilian 的解决方案。
  • @Underdog:不客气。虽然我不明白接受的答案如何有助于实现您想要的结果:“为了验证,我只想使用数字 1-9”
【解决方案2】:

您的问题是,IsNullOrWhiteSpace 仅检查 null,而不检查字符 '0'。如果你也想检查数字,你需要另外检查textBox5.Text.Equals("0")

if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Equals("0")) 
{
    MessageBox.Show("Textbox Cannot Empty or digit 0");
    textBox5.Focus();
}
else
{
    MessageBox.Show("Success!");
}
e.Handled = true;

编辑:这是.NET Fiddle


问:

如果输入的数字是 00000 怎么办?

答:

  1. 您可以像Tim Schmeltersuggestion 一样使用int.TryParse

  2. 或者您可以使用以下正则表达式进行验证:\^0*$\

再次.NET Fiddle

这里你需要:using System.Text.RegularExpressions;

if (string.IsNullOrWhiteSpace(textBox5.Text) || Regex.Match(textBox5.Text, "^0*$").Success) 
{
    MessageBox.Show("Textbox Cannot Empty or digit 0");
    textBox5.Focus();
}
else
{
    MessageBox.Show("Success!");
}
e.Handled = true;

【讨论】:

    【解决方案3】:
    if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Length == 0) 
                {
                    MessageBox.Show("Textbox Cannot Empty or digit 0");
                    textBox5.Focus();
                }
                else
                {
                    MessageBox.Show("Success!");
                }
    

    在这段代码中,您正在检查文本长度是否为 0

    // "" will have length 0 
    // "0" will have length 1 
    

    如果您想检查该框中是否有数字 0,您需要检查以下内容:

    if (string.IsNullOrWhiteSpace(textBox5.Text) || 
        textBox5.Text == "0")
    
    textBox5.Text.Length == 0 // you don't need this anymore if youre using IsNullOrWhiteSpace as IsNullOrWhiteSpace checks for null, string.Empty, white spaces
    

    当然,最好的检查方法是尝试解析 textBox5.Text 并使用以下代码查看是否得到 1 到 9 之间的数字:

    int.TryParse(textBox5.Text, out number) && number > 0 && number < 10 
    

    【讨论】:

      【解决方案4】:
      private void textBox5_TextChanged(object sender, EventArgs e)
              {
                  string[] removeCaracter = { "0",... };
                  foreach (var item in removeCaracter)
                  {
                      textBox5.Text = textBox5.Text.Replace(item, "");
                      textBox5.SelectionStart = textBox5.Text.Length ;
                      textBox5.SelectionLength = 0;
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-18
        • 2017-03-16
        • 2016-08-13
        • 1970-01-01
        相关资源
        最近更新 更多