winform的listbox问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 03:37:04
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
MessageBox.Show(listBox1.SelectedItems[x].ToString());
}
为什么总提示数组越界。。那要怎样才能输出items的内容啊?

这样写
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
MessageBox.Show(listBox1.Items[x-1].ToString());
}

listBox1.SelectedItems必须等你选中某些条记录才有东西否则是空的count是0
所以你越界了