开发者

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

If I have a list of strings, eg:

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

Is there an easy 开发者_StackOverflowway 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜