CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2009
    Posts
    5

    Smile How do i output an array to a ListBox?

    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.

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: How do i output an array to a ListBox?

    So you want to add all of the items in a given array to a ListBox control I take it?
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Jun 2009
    Posts
    5

    Re: How do i output an array to a ListBox?

    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).

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: How do i output an array to a ListBox?

    Your in luck Microsoft has a nice little function that you can do just that with:

    Code:
    myListBox.Items.AddRange(myArray);
    There you go, thats it. Piece of cake

    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.
    Last edited by RaleTheBlade; June 23rd, 2009 at 08:30 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    Jun 2009
    Posts
    5

    Re: How do i output an array to a ListBox?

    Thank you very much, you rock 8).

  6. #6
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: How do i output an array to a ListBox?

    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.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  7. #7
    Join Date
    Jun 2009
    Posts
    5

    Re: How do i output an array to a ListBox?

    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;.

  8. #8
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: How do i output an array to a ListBox?

    Wow, I didnt even know you could do that. Awesome... we both learned something new today, lol.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  9. #9
    Join Date
    Jun 2009
    Posts
    5

    Re: How do i output an array to a ListBox?

    Thx again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured