Input string was not in a correct format in double.Parse

前端 未结 5 1483
遥遥无期
遥遥无期 2020-12-07 02:50

I am new to C#. I\'m trying to make a calculator, but the following error occurred:

Input string was not in a correct format.

T

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 03:31

    Since you have cleared the textbox on the previous line, the Parse conversion fails.

    textBox1.Text = String.Empty;
    num2 = double.Parse(textBox1.Text);
    

    How will it convert String.Empty to Double? The way of doing it is not right. For example, if the "+" button is clicked, you have to check whether there was already a number. If so,, add the numbers and display the result:

    Double num;
    private void Add_Click(object sender, EventArgs e)
    {
    
       If (num != null)
       {
        num == num + Convert.ToDouble(textBox1.Text);
       }
       else
       {
        num1 == Convert.ToDouble(textBox1.Text);
       } 
       textBox1.Text = num;
    }
    

提交回复
热议问题