If I have a list of strings, eg:
List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
Is there an easy way to populate a ListBox using the contents of MyList?
Try :
List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
listBox1.DataSource = MyList;
Have a look at ListControl.DataSource Property
您还可以使用AddRange
方法
listBox1.Items.AddRange(myList.ToArray());
这是你想要的:
myListBox.DataSource = MyList;
This also could be easiest way to add items in ListBox.
for (int i = 0; i < MyList.Count; i++)
{
listBox1.Items.Add(MyList.ElementAt(i));
}
Further improvisation of this code can add items at runtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.