我對C#來說很新,我一直在尋找一個小時左右的時間來找到我需要的,但找不到它。我正在試圖查看32個不同文本框的內容,使用for循環。 我此刻的代碼是:C#使用for循環來處理WinForm環境中的多個元素
private void btnCalculate_Click(object sender, EventArgs e)
{
string ElementString;
Control ElementControl;
double Num;
Boolean errorMsg = false;
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
{
ElementString = "txtA" + x.ToString() + y.ToString();
ElementControl = this.Controls[ElementString];
ElementString = ElementControl.Text.Trim();
if (!double.TryParse(ElementString, out Num))
{
errorMsg = true;
break;
}
}
if (errorMsg)
MessageBox.Show("Error Processing Input Matricies, invalid entries");
}
好的改變這一部分,很抱歉不能把更多的信息,但希望這會有所幫助。
單擊按鈕時程序崩潰。這條線時崩潰:
ElementString = ElementControl.Text.Trim();
並給出錯誤信息: Object reference not set to an instance of an object.
謝謝
「它不工作」(原文如此)是*永遠*足夠的信息。請閱讀http://tinyurl.com/so-list –
可能的重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception -and-how-do-i-fix-it) –