?
Solved

How to Populate a Listbox from an array

Posted on 2013-11-20
5
Medium Priority
?
455 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 1200 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 400 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 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