-2

I"m trying to add items to my combobx but it"s not working:

static ArrayList bandlist = new ArrayList();

public addBand()
{
    InitializeComponent();
    bandlist[0] = "test";
    bandlist[1] = "test";
    fillComboBox();
}



public void fillComboBox()
{
    foreach (string item in bandlist)
    {
        combo.Text = item;
    }
}

Thank you

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jamie
  • 10,302
  • 32
  • 103
  • 186

4 Answers4

1

To add items to your combobox, you have to fill the .Items property (which is a collection). See https://msdn.microsoft.com/en-us/library/aa983551(v=vs.71).aspx

You're currently using the Text property:

Setting the Text property to null or an empty string ("") sets the SelectedIndex to -1. Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged.

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

You are setting the wrong property. You should set the Items Or DataSource property for entering all the values inside the control.

    combo.DataSource= bandlist;

OR

    foreach (var item in bandlist)
    {
      combo.Items.Add(item);
    }
Schuere
  • 1,579
  • 19
  • 33
0

Maybe you need:

combo.Items.Add(item);
  • Thankyou but when I do this it's empty. I"m receiving it now from an array but it's empty very strange. – Jamie Jun 29 '15 at 12:08
  • Seems like I was in a hurry. Check this out: [link](http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source) – malhotraprateek Jun 29 '15 at 12:11
0

To add an item to the combobox you should use the Items.Add method.

combo.Items.Add(item);

Also, I note that you use an ArrayList. Unless you're using the version 1 of the framework, I'd recommand to use a generic collection such as List<string>.

Look at ArrayList vs List<> in C# to see advantages of generic collections over ArrayList

Community
  • 1
  • 1
Tom C.
  • 593
  • 4
  • 12