Click to See Complete Forum and Search --> : How do i output an array to a ListBox?
wixxel
June 23rd, 2009, 08:22 PM
I apologize, i'm very new to C# and i can't find a resource to help me iterate and output an array to a Listbox in C#. Am i correct to assume that a Listbox is the correct control? Any help would be much appreciated.
RaleTheBlade
June 23rd, 2009, 08:23 PM
So you want to add all of the items in a given array to a ListBox control I take it?
wixxel
June 23rd, 2009, 08:25 PM
That is correct, i have my array created. Now i want to iterate through and output the array to a listbox ( or whatever control is more appropriate).
RaleTheBlade
June 23rd, 2009, 08:27 PM
Your in luck :D Microsoft has a nice little function that you can do just that with:
myListBox.Items.AddRange(myArray);
There you go, thats it. Piece of cake :thumb:
EDIT: After playing with the function a little more... it seems as though doing AddRange() with an array of value types (int, double, long, etc...) throws an error. But that shouldnt be the case since every object in C#, including value types, inherit from System.Object and AddRange() expects an array of objects.
wixxel
June 23rd, 2009, 08:28 PM
Thank you very much, you rock 8).
RaleTheBlade
June 23rd, 2009, 08:37 PM
No problem, I edited my post though so you may want to check it out to get a heads up on any future endeavors like this.
wixxel
June 23rd, 2009, 08:39 PM
Yes, i received an exception. But after searching with your input i was able to find another solution that worked fine for my code: myListBox.DataSource = myArray;.
RaleTheBlade
June 23rd, 2009, 08:41 PM
Wow, I didnt even know you could do that. Awesome... we both learned something new today, lol.
wixxel
June 23rd, 2009, 08:43 PM
Thx again.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.