Solved

How to Populate a Listbox from an array

Posted on 2013-11-20
5
452 Views
Last Modified: 2013-11-20
I have an array named MyArray.
The ListBox is unbound and named MyListBox.

MyArray(1).Name = "ABC";  
MyArray(2).Name = "DEF";
MyArray(3).Name= "GHI";

// I know that (x) should be [x].

I need to populate the listbox from the array items

//for (each array item)
for (int i =1; i = MyArray.Count(); ++i)
{
       Set the listbox item = the array item ?? Syntax
}

Thanks
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 39661851
Try something along the lines of:
string[] myArray = new string[] { "ABC", "DEF", "GHI" };

for (int i = 0; i < myArray.Length; ++i)
{
    ListBox1.Items.Add(myArray[i]);
}

Open in new window

If MyArray is something other than a intrinsic array, then you'll need to post the definition, or  tell us what type of object it contains.
0
 
LVL 11

Assisted Solution

by:Visio_Guy
Visio_Guy earned 100 total points
ID: 39661854
Hi Dovberman,

You are close. Here's a snippet that showcases two ways to do this. One is with a loop, the other uses data binding.

Data binding is nice, because list controls can understand collections of stuff to a certain extend (IEnumerable objects) so you don't have to do much work, plus the items in the list box are actually linked to the collection, not just copies of the data.



            
List<string> MyArray = 
new List<string>() {"ABC", "DEF", "GHI"};

// Clear the listbox:
this.listBox1.Items.Clear();

// Method 1: Populate the listbox using a loop:            
foreach (string s in MyArray)
{
     this.listBox1.Items.Add(s);
}

// Method 2: Populate the lisbox using binding:
this.listBox1.DataSource = MyArray;

// Note: for data binding, MyArray should probably have 
// form-wide scope, and not be defined in this procedure.

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39661876
The loop method works.

  for (int i = 0; i < arrFiles.Count(); ++i)
             {
                 lstFilesToDo.Items.Add(arrFiles(i).FileName);
             }

I prefer data binding.
MyArray has 2 string columns: FileName and FilePath.

Only the first column needs to be listed.

I will try it.

Thanks
0
 

Author Closing Comment

by:Dovberman
ID: 39661885
The second column is the path which is constant.  I will stay with the loop method and possibly use the data bind method for a different case.

Thanks
0
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 39661917
You might consider turning your rows of data into objects, then having a 1D array/collection of those objects.

Consider this class. It replaces the need to have secondary dimensions in your array:

    public class StringObject
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public StringObject(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }

Open in new window

Now, with data binding, you can pick out which property of the object to display:

// Make a 1-D array of StringObject objects:
StringObject[] array = new StringObject[]
         { new StringObject("One", "ABC"),
          new StringObject("Two", "DEF"),
          new StringObject("Three", "GHI") };

// Clear the listbox:
this.listBox1.Items.Clear();

// Populate the lisbox using binding:
this.listBox1.DataSource = array;

// Choose a display member (uncomment a line to show
// either Name or Value in the listbox:        
//this.listBox1.DisplayMember = "Name"; // ie: StringObject.Name
this.listBox1.DisplayMember = "Value"; // ie: StringObject.Value

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question