It's pretty simple to programmaticall y add items to a combobox at runtime. Just declare the object, then call ComboBox.Items. Add(...) for each item you wish to add. The one downside to this method is that you cannot specify values for the display member - the text shown in the ComboBox item - and the value member - the value returned when you query the combobox's SelectedValue property.
The simple way round this is to bind the combobox to a collection of objects. When binding a combobox, you specify the source (the collection), the name of the display member and the name of the value member. The names you specify correspond to the names of the properties of the object.
So, we need to create a two classes; one for each item in the combobox, and another generic collection class to group them all together and serve as the data source. Let's get started.
In my example, the item class represents a state. Each state has a long name, and a short name, as used in printed street addresses. The class just contains two properties and a simple constructor.
Now that the item class is defined, I also need a collection class. By inheriting the CollectionBase class, I can access the inbuilt methods for adding, removing and sorting objects. I can also use the collection class constructor to populate the collection and sort the items.
The child class, StatesComparer, handles the sorting of the items. As each item is a custom class, the method of sorting the items needs to be defined. As the CollectionBase' s Innerlist property is an Arraylist, the class used to define the sorting needs to implement the IComparer interface. The sorting class presents just one function to its parent, Compare. The function takes two parameters - two different items in the collection - and returns either true or false depending on whether the first parameter is less than (appears in the collection before) the second parameter.
Now I have my populated and sorted collection of items, all I need to do is to bind my combobox to it. It doesn't really matter where one does this, provided it is called before the combobox is needed.
The simple way round this is to bind the combobox to a collection of objects. When binding a combobox, you specify the source (the collection), the name of the display member and the name of the value member. The names you specify correspond to the names of the properties of the object.
So, we need to create a two classes; one for each item in the combobox, and another generic collection class to group them all together and serve as the data source. Let's get started.
In my example, the item class represents a state. Each state has a long name, and a short name, as used in printed street addresses. The class just contains two properties and a simple constructor.
Code:
Public Class State Private _ShortName As String Public Property ShortName() As String Get Return _ShortName End Get Set(ByVal value As String) _ShortName = value End Set End Property Private _LongName As String Public Property LongName() As String Get Return _LongName End Get Set(ByVal value As String) _LongName = value End Set End Property Public Sub New(ByVal shortName As String, ByVal longName As String) Me.ShortName = shortName Me.LongName = longName End Sub End Class
Code:
Public Class States
Inherits CollectionBase
Public Sub New()
InnerList.Add(New State("QLD", "Queensland"))
InnerList.Add(New State("NSW", "New South Wales"))
InnerList.Add(New State("VIC", "Victoria"))
InnerList.Add(New State("TAS", "Tasmania"))
InnerList.Add(New State("WA", "Western Australia"))
InnerList.Add(New State("SA", "South Australia"))
InnerList.Add(New State("NT", "Northern Territory"))
InnerList.Add(New State("ACT", "Australian Capital Territory"))
InnerList.Sort(New StatesComparer)
End Sub
Friend Class StatesComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
If TypeOf (x) Is State And TypeOf (y) Is State Then
Return DirectCast(x, State).LongName < DirectCast(y, State).LongName
Else
Throw New Exception("Object is not of type State")
End If
End Function
End Class
End Class
Now I have my populated and sorted collection of items, all I need to do is to bind my combobox to it. It doesn't really matter where one does this, provided it is called before the combobox is needed.
Code:
Dim comboStates As New ComboBox() comboStates.DataSource = New States comboStates.DisplayMember = "LongName" comboStates.ValueMember = "ShortName"