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
Solved

How to Populate a Listbox from an array

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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