Solved

How to Populate a Listbox from an array

Posted on 2013-11-20
5
447 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

785 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