验证文本框上的数字输入

时间:2013-10-28 17:34:22

标签: c# winforms

我想在Windows窗体应用程序的文本框上验证输入,例如,如果输入的数字小于1或大于24 ,或者输入除整数之外的任何其他字符,则显示带错误的消息框即可。我该怎么做呢?

5 个答案:

答案 0 :(得分:3)

我会考虑这样的事情:

    //make sure that we have a valid number in the text box with no other characters       
    //loop through the string, examining each character
    for (int i = 0; i < txtHour.Text.Length; i++)
    {
        //if this character isn't a number, then don't close the form or continue           
        if (!char.IsNumber(txtHour.Text[i]))
        {
            MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
            return;
        }
    }
    //now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24
    int testInt = Convert.ToInt32(txtHour.Text);
    if (testInt < 1 || testInt > 24)
    {
        MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
        return;
    }

对于您在评论中要求的方法示例,您可以执行更多类似的操作:

    //////////////////////////////////////////////////////////////////////
    //in your main code:
    if (!isValidHour(textBox1.Text))
        MessageBox.Show("Value for field must be a number from 1 to 24");

    if (!isValidHour(textBox2.Text))
        MessageBox.Show("Value for field must be a number from 1 to 24");

    //////////////////////////////////////////////////////////////////////

    ///method to validate if text field is an INT from 1 to 24  
    bool isValidHour (string stringToValidate)
    {
        //make sure that we have a valid number in the text box with no other characters       
        //loop through the string, examining each character
        for (int i = 0; i < stringToValidate.Length; i++)
        {
            //if this character isn't a number, then don't close the form or continue           
            if (!char.IsNumber(stringToValidate[i]))
            {
                //MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
                return false;
            }
        }
        //now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24
        int testInt = Convert.ToInt32(stringToValidate);
        if (testInt < 1 || testInt > 24)
        {
            //MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
            return false;
        }

        return true;
    }

答案 1 :(得分:1)

try{
  if((int)item.value >= 1 && (int)item.value <=25){
    //match.
  }else{
    //error.
  }
}catch (Exception e){
  //type error
}

//or---

var itemValue = default(int);
if(int.TryParse(item.value, out itemValue)){
  if(itemValue >= 1 && itemValue <= 25){
    //match.
  }else{
    //error.
  }
}else{
  //item.value is not numeric.
}

答案 2 :(得分:1)

您可以通过传递需要验证的文本框控件来添加私有方法并在必要时调用它。

 private void ValidateText(TextBox textbox)
        {
            int value;

            bool isConverted = Int32.TryParse(textbox.Text.Trim(), out value);
            if (!isConverted)
            {
                MessageBox.Show("Only numbers allowed");
                return;
            }

            if (value < 1 || value > 24)
            {
                MessageBox.Show("Please enter a value between 1-24");
            }
        }

Validating txtHour by invoking above method

ValidateText(txtHour);

答案 3 :(得分:0)

查看使用OnKeyPress / OnKeyDown / OnKeyUp事件进行检查:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

答案 4 :(得分:0)

这是一个非常简单的实现,但是,这将实现您要求的验证:

int hoursEntered;
bool isInteger;
isInteger = int.TryParse(txtHour.Text, out hoursEntered);

if (hoursEntered < 1 || hoursEntered > 24 && isInteger == true)
   MessageBox.Show("Your number is either less than 1, greater than 24 or you didn't enter a number", "Warning!", MessageBoxButtons.OK);  

但这里有一些事情需要考虑。我编写了这段代码并将其绑定到按钮单击的事件处理程序。如果那不是您的实现,那么我强烈考虑使用OnKeyPress/OnKeyUp/OneKeyDown事件来进行验证。否则,您只需将此代码复制到就地已有的按钮单击事件处理程序中。