Solved

How to Populate a Listbox from an array

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now