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

白昼怎懂夜的黑 提交于 2019-11-30 01:26:54
Adriaan Stander

Try :

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

listBox1.DataSource = MyList;

Have a look at ListControl.DataSource Property

You can also use the AddRange method

listBox1.Items.AddRange(myList.ToArray());

Is this what you are looking for:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!