2012-04-23 62 views
-1

我收到错误“格式异常未处理”。和“输入字符串格式不正确”。它在这一行temp_i = float.Parse(textBox3.Text);问题是什么?格式异常未处理。输入字符串格式不正确

//button 2 calculate button 
private void button1_Click(object sender, EventArgs e) 
{ 
    float temp_e; 
    float temp_i; 
    float temp_r; 
    float temp_p; 

    //******************************************************* 
    // Resistance = Volts/Current 
    //******************************************************* 
    if (IsNumeric(textBox1.Text) && 
    IsNumeric(textBox2.Text) && 
    textBox3.Text == ("")) 
    { 
    temp_e = float.Parse(textBox1.Text); //convert string to number 
    temp_i = float.Parse(textBox3.Text); //convert string to number 

    temp_r = temp_e/temp_i; //display 1st result 
    textBox2.Text = Convert.ToString(temp_r); //post result resistance (R) 

    //calculate power 
    temp_p = temp_e * temp_i; 
    textBox5.Text = Convert.ToString(temp_p); 

    //display 2nd result 
    textBox4.Text = Convert.ToString(temp_r) + " * " + Convert.ToString(temp_i) + " = " + Convert.ToString(temp_p) + " watts"; 
    }' 
+0

你会怎样想转换“”浮动?你确定 ?尝试删除该条件,否则解析并比较它。 – Milee 2012-04-23 11:02:17

回答

4
temp_i = float.Parse(textBox3.Text); //convert string to numbe 

textBox3.Text肯定包含 “”,因为它是你的,如果条件。

你不能解析“”浮动。

+0

谢谢你的回答。这个答案纠正了我的问题。 – Hrfpkj 2012-04-23 11:06:55

0

问题应该很明显; textbox3.Text包含一个无效的值,传递给float.Parse。在你的情况,一个空字符串(基于if它上面!

0

您检查文本框是否为空,如果是,你想转换为浮动?检查你的逻辑。

我我猜你是想做到这一点:

temp_i = float.Parse(textBox2.Text); 

这会更有意义:)