2010-12-03 51 views
0

我想顯示按鈕單擊列表。我在.xaml文件中添加了一個列表框,並希望在列表中添加10個文本框。以下代碼顯示錯誤。無法顯示列表?kam

 private void listbutton_C(object sender, RoutedEventArgs e) 
     { 
     String str = "thumb_"; 
     TextBox[] name = new TextBox[20]; 
     for (int i = 1; i < 11; i++) 
     { 

      if (i == 10) 
      { 
       strPath = str + "0" + i + ".jpg"; 
      } 
      else 
      { 
       strPath = str + "00" + i + ".jpg"; 
      } 

      name[i].Text = strPath; 
      listBox1.Items.Add(name[i]); 
     } 


     ContentPanel2.Visibility = Visibility.Collapsed; 
     listBox1.Visibility = Visibility.Visible; 
    } 

名[I]的.text = strPath的顯示NullReferenceException異常。可有人解釋這是什麼問題?

回答

1

我認爲你需要實例化每個文本框,你只創建了數組。

for (int i = 1; i < 11; i++) 
    { 
     name[i] = new TextBox(); // insert this line 
     if (i == 10) 
     { 
      strPath = str + "0" + i + ".jpg"; 
     } 
     else 
     { 
      strPath = str + "00" + i + ".jpg"; 
     } 

     name[i].Text = strPath; 
     listBox1.Items.Add(name[i]); 
    } 
+0

thanx很多..它完美的工作。 – Shaireen 2010-12-03 10:33:52