cannot display a list ?kam
I want to dispaly a list on a button click.I have added a list box in .xaml file and want to add 10 text boxes in list.The following code shows errors.
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
开发者_StackOverflow中文版 {
strPath = str + "00" + i + ".jpg";
}
name[i].Text = strPath;
listBox1.Items.Add(name[i]);
}
ContentPanel2.Visibility = Visibility.Collapsed;
listBox1.Visibility = Visibility.Visible;
}
name[i].text=strpath show nullreferenceExceptions .Can somebody explain what is the problem?
I think you need to instantiate every textbox, you have only created the array.
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]);
}
精彩评论