如图 ListBox 有4条数据,我想把它里面的数据存进后台的数组里做循环添加,应该怎么写啊?
string []x=??
说明下是C#桌面程序
第1个回答 2012-09-12
//new 一个长度为 listBox1.Items.Count的数组
int[] a = new int[listBox1.Items.Count];
//循环便利listBox1中的每一项
for (int i = 0; i < listBox1.Items.Count; i++)
{
//赋值给数组
a[i] = Convert.ToInt32(listBox1.Items[i]);
//MessageBox.Show(a[i].ToString());
}本回答被提问者和网友采纳
int[] a = new int[listBox1.Items.Count];
//循环便利listBox1中的每一项
for (int i = 0; i < listBox1.Items.Count; i++)
{
//赋值给数组
a[i] = Convert.ToInt32(listBox1.Items[i]);
//MessageBox.Show(a[i].ToString());
}本回答被提问者和网友采纳
第2个回答 2012-09-12
不要用数组,用List<string>
List<string> listStr = new List<string>();
foreach (var v in listBox1.Items)
{
listStr.Add(v.ToString());
}
这样有个好处,无论listBox里有几条数据都可以取出
listStr可以像普通数组一样使用
listStr[0]=***;
List<string> listStr = new List<string>();
foreach (var v in listBox1.Items)
{
listStr.Add(v.ToString());
}
这样有个好处,无论listBox里有几条数据都可以取出
listStr可以像普通数组一样使用
listStr[0]=***;
第3个回答 2012-09-12
listBox1.DataSource