C#: easiest way to populate a ListBox from a List

Craig Johnston picture Craig Johnston · Dec 1, 2010 · Viewed 129.3k times · Source

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?

Answer

Adriaan Stander picture Adriaan Stander · Dec 1, 2010

Try :

List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");

listBox1.DataSource = MyList;

Have a look at ListControl.DataSource Property