2012-10-02 85 views
1

這是我的代碼:奇怪的錯誤(輸入的字符串格式不正確。)

namespace Class_Properties { 
    public partial class Form1 : Form { 
     private string firstHeight1 = ""; 
     public int firstHeight { 
      get { 
         return Convert.ToInt32(firstHeight1); 
      } 
     } 

     public Form1() { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) { 
      firstHeight1 = textBox2.Text; 

      Form2 secondForm = new Form2(); 
      secondForm.Show(); 
     } 
    } 
} 

,然後其他類:

namespace Class_Properties { 
    public partial class Form2 : Form { 
     public Form2() { 
      InitializeComponent(); 
      Form1 mainWindow = new Form1(); 
      this.Height = mainWindow.firstHeight; 
     } 
    } 
} 

當我跑,我鍵入200作爲價值textbox2並單擊button1,然後Visual Studio中說,以下情況除外:

enter image description here

我能做些什麼來解決這個錯誤?

回答

1

這是失敗:

 InitializeComponent(); 
     Form1 mainWindow = new Form1(); 
     this.Height = mainWindow.firstHeight; //<-- 

無論你在其他Form1上做了什麼,它都不會出現在這一個中,因爲它是一個新實例,所以firstHeight == string.Empty將會失敗解析。

你必須對現有的Form1中發送到窗體2:

public Form2(Form1 parent) 
{ 
    this.Height = parent.firstHeight; 
} 

// called like so from Form1: 
var form2 = new Form2(this); 

但無可否認,這將是最好只發送你所需要的:

public Form2(int desiredHeight) 
{ 
    this.Height = desiredHeight; 
} 

// called like so from Form1: 
var form2 = new Form2(this.firstHeight); 
+0

這個工作...所以我必須明白,我可以通過將Form1作爲參數傳遞給Form2()... ... – Victor

+0

@維克多:我真的建議只發送你需要的Form2而不是Form1本身。如果可以的話,耦合是一種避免的習慣,並且基於你的例子,Form2真的不需要了解Form1。 –

+0

好的。非常感謝你! :) – Victor

0

firstHeight1引發異常時的值是什麼?

你可能想看看int.TryParse()代替:

int output = 0; 
int.TryParse(firstHeight1, out output); 
return output; 

如果無法分析值,也不會設置output,而不是拋出一個異常。

上int.TryParse更多信息:http://msdn.microsoft.com/en-us/library/f02979c7.aspx

編輯:問題是你重新實例化Form1中和值永遠不會在Form1的新實例,距離窗體2。您應該將Form2中的屬性設置爲該值。

class Form2 
{ 
    public int FirstHeight { get; set; } 
} 

...

Form2 form2 = new Form2(); 
form2.FirstHeight = this.FirstHeight; 
form2.Show(); 
+0

OP說價值是'200' ... – Servy

0

由於您firstHeight1String.Empty並沒有類似空字符串可以Int類型中找到。因此,錯誤......

在您的Form2實例中,值仍然是String.Empty。首先將其設置爲某個值。

0

Form2當你說:

Form1 mainWindow = new Form1(); 
this.Height = mainWindow.firstHeight; 

您沒有訪問Form1您已經使用較早,你要創建一個全新的具有空文本框的值。

什麼,你應該做的是經過高度值到第二種形式創建時:

public Form2(int height) 
{ 
    // use height here 
} 

,並點擊按鈕:

Form2 secondForm = new Form2(firstHeight); 
secondForm.Show(); 
0

你可以做這樣的事情。

public int? FirstHeight 
{ 
    get 
    { 
     int? returnValue = null; 

     int temp; 
     if (int.TryParse(textBox2.Text, out temp)) 
     { 
      returnValue = temp; 
     } 

     return returnValue; 
    } 
} 

然後你只需撥打FirstHeight屬性,當你需要的值:

if (FirstHeight.HasValue) 
{ 
    // Access the int value like so: 
    int height = FirstHeight.Value; 
} 

如果你不想使用可空類型,你可以這樣做,而不是:

public int FirstHeight 
{ 
    get 
    { 
     int returnValue; // Or some other default value you can check against. 

     if (!int.TryParse(textBox2.Text, out returnValue)) 
     { 
      // If the call goes in here, the text from the input is not convertable to an int. 
      // returnValue should be set to 0 when int.TryParse fails. 
     } 

     return returnValue; 
    } 
} 
0

你的代碼看起來像

public int FirstHeight 
{ 
    get 
    { 
     double Num; 
     bool isNum = double.TryParse(firstheight1, out Num); 
     if (isNum) 
    { 
     return firstheight1; 
     } 
    else 
    { 
    return 0; or 
    your message goes here 
    } 

} }