這是我的代碼:奇怪的錯誤(輸入的字符串格式不正確。)
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中說,以下情況除外:
我能做些什麼來解決這個錯誤?
這個工作...所以我必須明白,我可以通過將Form1作爲參數傳遞給Form2()... ... – Victor
@維克多:我真的建議只發送你需要的Form2而不是Form1本身。如果可以的話,耦合是一種避免的習慣,並且基於你的例子,Form2真的不需要了解Form1。 –
好的。非常感謝你! :) – Victor