2012-04-23 48 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); 

這會更有意義:)