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

前端 未结 5 1486
遥遥无期
遥遥无期 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:42

    Seems like num2 value should be fetched from textbox2 not textbox1(You are setting textbox1.text to empty and trying to parse it to double again)

    //You are setting textbox1 to empty
    textBox1.Text = String.Empty;
    
    //here trying to parse it to double
    num2 = double.Parse(textBox1.Text);
    

    Also don't use Convert.ToDouble(textBox1.Text) directly. if users type non numeric values your code will crash. first check if its a valid number, always use doube.TryPrase()

    double num1;
    double.TryParse(textBox1.Text, out num1);
    

提交回复
热议问题