2015-10-02 49 views
-6

我正在製作一個c-sharp應用程序來轉換距離單位。 我的應用程序提示錯誤:「輸入字符串格式不正確」。由於我是新的語言,我將不勝感激。錯誤輸入字符串的格式不正確

這是我使用的代碼:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedIndex == 0) { 
       label2.Text = "Miles"; 
       label3.Text = "kilometers"; 

       double m; 
       double kilometer=1.6093 ; 

       m = kilometer * Convert.ToDouble(textBox1.Text);//here is the problem 
       textBox2.Text = m.ToString(); 
+4

你檢查了字符串是什麼嗎? – SLaks

+0

你應該[編輯]你的問題,將錯誤添加爲文本而不是截圖。你的問題還應該包括哪些輸入導致了錯誤。 textBox1中有什麼? – BSMP

+0

看起來像'textBox1.Text'的值不是以有效格式轉換爲double。 – user1666620

回答

1

我覺得正是你需要先檢查您的文本框的值,因爲你不能老是轉換空值到雙

if (textBox1.Text!="") 
       { 
        m = kilometer * Convert.ToDouble(textBox1.Text);//here is the problem 
        textBox2.Text = m.ToString(); 
       } 

或者在開始計算之前給textBox1初始值, 注意:如果使用TextBox1的數字Regex這會更好 謝謝。