简体   繁体   English

C#Windows窗体FormatException未处理

[英]C# Windows Form FormatException was unhandled

I am trying to put a check in textbox for value not greater then 100 but i am getting formatExeception can anybody help in this regard.. Thanks in Advance. 我试图在文本框中输入不大于100的值,但是我在使用formatExeception可以在这方面提供任何帮助。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (Int32.Parse(textBox1.Text) > 100)
    {
        MessageBox.Show("No. Of Elements Must be Less Then 100");
    }
}

You need to surround your statement with a try-catch in case the content of the textbox cannot be parsed into Int32. 如果文本框的内容无法解析为Int32,则需要用try-catch包围语句。 Assuming you run into the exception, you can then get a message describing the reason for the error. 假设您遇到了异常,则可以获取一条描述错误原因的消息。

Probably better to use a Numeric Updown if you only want the user to enter numbers but the error is happening because the text in the textbox cannot be parsed to a number. 如果只希望用户输入数字,但由于无法将文本框中的文本解析为数字而发生错误,则最好使用数字上移。 Use int.TryParse. 使用int.TryParse。 It wont throw an exception if it cannot parse the string to a number 如果无法将字符串解析为数字,则不会抛出异常

        int numElements = 0;
        int.TryParse(textBox1.Text, out numElements);
        if(numElements >100){
            MessageBox.Show("No. Of Elements Must be Less Then 100");
        }
private void textBox1_TextChanged(object sender, EventArgs e)
{
    int parsed = 0;
    if (!int.TryParse(textBox1.Text), out parsed)
    {
        MessageBox.Show("No. You must enter a number!");
        return;
    }
    if (parsed > 100)
    {
        MessageBox.Show("No. Of Elements Must be Less Then 100");
    }
}

That property of Parse to throw exceptions on parsing error is quite Vexing . 解析引发解析错误时抛出异常的属性颇为烦人 It is so Vexing, the Framework Developers added TryParse with the 2.0 version. 正是这样,框架开发人员在2.0版本中添加了TryParse。 If you want to parse strings, you should always be using TryParse once you get past the initial development phase. 如果要解析字符串,则在经过初始开发阶段后就应该始终使用TryParse。

Or ideally a appraoch to validation/input that does not allow faulty inputs (like the Numerical Up/Down Ken Tucker noted. 或理想情况下,是一种验证/输入的方法,不允许有错误的输入(如Ken Tucker指出的数字上/下)。

If you somehow lack access to TryParse, I wrote a copy of it way back: 如果您因某种原因无法访问TryParse,则可以回写它的副本:

//Parse throws ArgumentNull, Format and Overflow Exceptions.
//And they only have Exception as base class in common, but identical handling code (output = 0 and return false).

bool TryParse(string input, out int output){
  try{
    output = int.Parse(input);
  }
  catch (Exception ex){
    if(ex is ArgumentNullException ||
      ex is FormatException ||
      ex is OverflowException){
      //these are the exceptions I am looking for. I will do my thing.
      output = 0;
      return false;
    }
    else{
      //Not the exceptions I expect. Best to just let them go on their way.
      throw;
    }
  }

  //I am pretty sure the Exception replaces the return value in exception case. 
  //So this one will only be returned without any Exceptions, expected or unexpected
  return true;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM