I am using C# with a Windows Application Form. In this I have a combobox. What is the code to add the dropdown selections? From my googling so far I presume I need to setup an arraylist for the details?
To add Items to the ComboBox you have two options:
Either add them to the Items collection:
comboBox1.Items.Add("abc");
comboBox1.Items.Add("def");
Or use data binding:
comboBox1.DataSource = myList;
or with an array:
comboBox1.DataSource = myArray;
For the first variant you can only use strings as items, while with data binding you can bind a collection of more complex objects. You can then specify what properties are displayed:
comboBox1.DisplayMember = "Name";
and what are treated as value:
comboBox1.ValueMember = "ID";
You can access the original object that is selected later with
comboBox1.SelectedItem
or the value with
comboBox1.SelectedValue
The value is the property you specified with ValueMember
.
You can use ComboBox1.Items.Add("Item") to add items 1 at a time, or ComboBox1.Items.AddRange(MyArray) to add a whole list of items at once. Each item that you add can be a string, in which case it is displayed directly in the dropdown list, or it can be an object, in which case the DisplayMember property of the combo box is used to determine which of the objects properties will appear in teh dropdown list.