【发布时间】:2011-05-18 07:17:18
【问题描述】:
如果我有一个字符串列表,例如:
List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
有没有一种简单的方法可以使用 MyList 的内容来填充 ListBox?
【问题讨论】:
如果我有一个字符串列表,例如:
List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
有没有一种简单的方法可以使用 MyList 的内容来填充 ListBox?
【问题讨论】:
试试:
List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
listBox1.DataSource = MyList;
【讨论】:
这也是在 ListBox 中添加项目的最简单方法。
for (int i = 0; i < MyList.Count; i++)
{
listBox1.Items.Add(MyList.ElementAt(i));
}
此代码的进一步即兴创作可以在运行时添加项目。
【讨论】:
你也可以使用AddRange方法
listBox1.Items.AddRange(myList.ToArray());
【讨论】:
这是你要找的东西吗:
myListBox.DataSource = MyList;
【讨论】: