0

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

4

5 回答 5

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;
    }
于 2013-10-28T19:27:48.690 回答
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.
}
于 2013-10-28T17:38:00.330 回答
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);
于 2013-10-29T09:07:43.733 回答
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事件来进行验证。否则,您可以将此代码复制到您已经拥有的任何按钮单击事件处理程序中。

于 2013-10-28T19:00:46.810 回答
0

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

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

于 2013-10-28T17:35:50.703 回答