Why am I getting a FormatException was unhandled error?

后端 未结 3 814
情话喂你
情话喂你 2020-12-12 01:51

I have created a program, and a extensive test of it, I\'m getting a error that says \"FormatException was unhandled, Input string was not in a correct format\". The problem

3条回答
  •  粉色の甜心
    2020-12-12 02:42

    The problem occurs when I leave either of the text boxes blank

    That's the issue. You are using int.Parse on an empty string. An empty string is not a valid integer representation and the parse fails.

    You can test for the value in the textbox and default to something reasonable instead:

    int minsEntered = 0;
    int secsEntered = 0;
    
    if(!string.IsNullOrWhitespace(textMins.Text))
    {
      minsEntered = int.Parse(txtMins.Text);
    }
    
    if(!string.IsNullOrWhitespace(txtSecs.Text))
    {
      secsEntered = int.Parse(txtSecs.Text);
    }
    

提交回复
热议问题