2010-12-03 64 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