ListBox.Items collection
-
Tuesday, March 04, 2008 6:58 PM
Could someone please provide the code to create a string object that can hold a list of strings that can then be added to a listbox control using the AddRange method, i.e. Listbox1.Items.AddRange(stringObject), where stringObject is some c# string collection that works with the AddRange method of a listbox. Any help would be appreciated. Thanks.
I had hoped that something like List<string> stuff = new List<string>();
stuff.Add("one");
stuff.Add("two");
stuff.Add("three");
Listbox1.Items.AddRange(stuff);
would work, but it does not.
Answers
-
Wednesday, March 05, 2008 4:03 AM
well you can store your strings in an array and then add those things to the list box using listbox.items.add
like this
Code Snippetprivate
void Form1_Load(object sender, EventArgs e){
string[] s = new string[5] { "hello", "world", "my", "you", "all" }; foreach (string ss in s){
listBox1.Items.Add(ss);
}
}
and there are many other ways also like you can databind the listbox
Code Snippetprivate
void Form1_Load(object sender, EventArgs e){
string[] s = new string[5] { "hello", "world", "my", "you", "all" };listBox1.BeginUpdate();
listBox1.Items.AddRange(s);
listBox1.EndUpdate();
}
-
Wednesday, March 05, 2008 4:16 AM
sorry friend i didn't understood what you were asking so here is the thing that you were looking for (i guess)
Code SnippetList
<string> l = new List<string>();l.Add(
"hello");l.Add(
"world");listBox1.Items.AddRange(l.ToArray());
i hope this helps
All Replies
-
Wednesday, March 05, 2008 4:03 AM
well you can store your strings in an array and then add those things to the list box using listbox.items.add
like this
Code Snippetprivate
void Form1_Load(object sender, EventArgs e){
string[] s = new string[5] { "hello", "world", "my", "you", "all" }; foreach (string ss in s){
listBox1.Items.Add(ss);
}
}
and there are many other ways also like you can databind the listbox
Code Snippetprivate
void Form1_Load(object sender, EventArgs e){
string[] s = new string[5] { "hello", "world", "my", "you", "all" };listBox1.BeginUpdate();
listBox1.Items.AddRange(s);
listBox1.EndUpdate();
}
-
Wednesday, March 05, 2008 4:16 AM
sorry friend i didn't understood what you were asking so here is the thing that you were looking for (i guess)
Code SnippetList
<string> l = new List<string>();l.Add(
"hello");l.Add(
"world");listBox1.Items.AddRange(l.ToArray());
i hope this helps -
Wednesday, March 05, 2008 4:20 AM
Bob C# newbie wrote: I had hoped that something like List<string> stuff = new List<string>();
stuff.Add("one");
stuff.Add("two");
stuff.Add("three");
Listbox1.Items.AddRange(stuff);
would work, but it does not.
the above thing won't work becuase the comiler won't be able to type cast the list collections to object collections and will through the error as such
"Error 2 Argument '1': cannot convert from 'System.Collections.Generic.List<string>' to 'System.Windows.Forms.ListBox.ObjectCollection' "
-
Wednesday, March 05, 2008 12:43 PMThis is exactly what I had in mind. Thanks.
-
Wednesday, March 05, 2008 12:48 PMThis works but do I have to commit ahead of time as to the size of the string array? If I do, then I like the List<string> solution better. Also, in this case, I see that I do not need the BeginUpdate() and EndUpdate() methods but I am glad you included them because, now that I know what they are used for, they may come in handy in situations where the size of the array is much larger. Thanks for your solution.

