- Items Collection
Public Class frmCollectionComboBox
Private Sub btnPopulateComboBox_Click(sender As Object, e As EventArgs) Handles btnPopulateComboBox.Click
On Error Goto Errorhandler
Me.ComboBox1.Items.Clear
Me.ComboBox1.Items.Add("Chrysler")
Me.ComboBox1.Items.Add("Dodge")
Me.ComboBox1.Items.Add("Ford")
Me.ComboBox1.Items.Add("GM")
Me.ComboBox1.Items.Add("Honda")
Me.ComboBox1.Items.Add("Hyundai")
Me.ComboBox1.Items.Add("Kia")
Me.ComboBox1.Items.Add("Nissan")
Me.ComboBox1.Items.Add("Toyota")
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnShowComboBoxText_Click(sender As Object, e As EventArgs) Handles btnShowComboBoxText.Click
On Error Goto Errorhandler
Msgbox(Me.ComboBox1.SelectedItem)
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnSetBlankComboBox_Click(sender As Object, e As EventArgs) Handles btnSetBlankComboBox.Click
On Error Goto Errorhandler
Me.ComboBox1.SelectedIndex = -1
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnIsComboBoxEmpty_Click(sender As Object, e As EventArgs) Handles btnIsComboBoxEmpty.Click
On Error Goto Errorhandler
If Me.ComboBox1.SelectedIndex = -1 Then
Msgbox("ComboBox1 is empty.")
Else
Msgbox("ComboBox1 is not empty.")
End If
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnIsComboBoxHyundai_Click(sender As Object, e As EventArgs) Handles btnIsComboBoxHyundai.Click
On Error Goto Errorhandler
If Me.ComboBox1.SelectedText = "Hyundai" Then
Msgbox("The selected ComboBox text is Hyundai.")
Else
Msgbox("The selected ComboBox text is not Hyundai.")
End If
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
End Class
- Class Driven
The Data Source Property of the ComboBox along with the DisplayMember and ValueMember Properties are used to populate a ComboBox with an ID and Text Value Pair.
- Write a Class that has a Property for the ID and a Property for the Text Value.
- Write a routine to initialize the Property Values of the Class.
- Create an instance of the Class for each Value in the ComboBox.
- Add each instance of the Class to a List that is of the Type of the Class.
- Bind the List to the Data Source Property of the Class.
- Set the DisplayMember Property of the ComboBox to the name of the Class Property that holds the Text Value.
- Set the ValueMember property of the ComboBox to the name of the Class Property that holds the ID Value.
There are many examples on the internet of how to bind a ComboBox to a DataSet. In my opinion, it is a better idea to use the Class driven approach described here instead of directly binding Controls on Forms to Recordsets or DataSets.
'Write a Class that has a Property for the ID and a Property for the Text Value.
'Write a routine to initialize the Property Values of the Class.
'--------------------------------------------------------------------------------------
Public Class cCarCompany
Private lCarCompanyID As Integer
Private lCarCompanyName As String
Public Property pCarCompanyID() As Integer
Get
pCarCompanyID = lCarCompanyID
End Get
Set(ByVal iCarCompanyID As Integer)
lCarCompanyID = iCarCompanyID
End Set
End Property
Public Property pCarCompanyName() As String
Get
pCarCompanyName = lCarCompanyName
End Get
Set(ByVal sCarCompanyName As String)
lCarCompanyName = sCarCompanyName
End Set
End Property
Public Function mInitializeCarCompanyClass( _
ByRef iCarCompanyID As Integer, _
ByRef sCarCompanyName As String) As Integer
On Error Goto Errorhandler
lCarCompanyID = iCarCompanyID
lCarCompanyName = sCarCompanyName
Exit Function
Errorhandler:
Msgbox (Err.Number & "-" & Err.Source & ": " & Err.Description)
End Function
End Class
'Create an instance of the Class for each Value in the ComboBox.
'Add each instance of the Class to a List that is of the Type of the Class.
'Bind the List to the Data Source Property of the Class.
'Set the DisplayMember Property of the ComboBox to the name of the Class Property that holds the Text Value.
'Set the ValueMember property of the ComboBox to the name of the Class Property that holds the ID Value.
'--------------------------------------------------------------------------------------
Public Class frmClassComboBox
Private Sub btnPopulateComboBox_Click(sender As Object, e As EventArgs) Handles btnPopulateComboBox.Click
On Error Goto Errorhandler
'Initialize Variables
'-------------------------------------------
Dim oCarCompanyList As List(Of cCarCompany)
Dim oCarCompany(8) As cCarCompany
oCarCompanyList = New List(Of cCarCompany)
'Populate Objects
'-------------------------------------------
oCarCompany(0) = New cCarCompany
oCarCompany(0).mInitializeCarCompanyClass(0, "Chrysler")
oCarCompanyList.Add(oCarCompany(0))
oCarCompany(1) = New cCarCompany
oCarCompany(1).mInitializeCarCompanyClass(1, "Dodge")
oCarCompanyList.Add(oCarCompany(1))
oCarCompany(2) = New cCarCompany
oCarCompany(2).mInitializeCarCompanyClass(2, "Ford")
oCarCompanyList.Add(oCarCompany(2))
oCarCompany(3) = New cCarCompany
oCarCompany(3).mInitializeCarCompanyClass(3, "GM")
oCarCompanyList.Add(oCarCompany(3))
oCarCompany(4) = New cCarCompany
oCarCompany(4).mInitializeCarCompanyClass(4, "Honda")
oCarCompanyList.Add(oCarCompany(4))
oCarCompany(5) = New cCarCompany
oCarCompany(5).mInitializeCarCompanyClass(5, "Hyundai")
oCarCompanyList.Add(oCarCompany(5))
oCarCompany(6) = New cCarCompany
oCarCompany(6).mInitializeCarCompanyClass(6, "Kia")
oCarCompanyList.Add(oCarCompany(6))
oCarCompany(7) = New cCarCompany
oCarCompany(7).mInitializeCarCompanyClass(7, "Nissan")
oCarCompanyList.Add(oCarCompany(7))
oCarCompany(8) = New cCarCompany
oCarCompany(8).mInitializeCarCompanyClass(8, "Toyota")
oCarCompanyList.Add(oCarCompany(8))
'Populate The ComoboBox
'-------------------------------------------
Me.ComboBox1.Items.Clear
Me.ComboBox1.DataSource = oCarCompanyList
Me.ComboBox1.DisplayMember = "pCarCompanyName"
Me.ComboBox1.ValueMember = "pCarCompanyID"
Me.ComboBox1.SelectedIndex = -1
'Clean Up Variables
'-------------------------------------------
oCarCompanyList = Nothing
oCarCompany = Nothing
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
'Typical ComboBox tasks.
'--------------------------------------------------------------------------------------
Private Sub btnShowComboBoxText_Click(sender As Object, e As EventArgs) Handles btnShowComboBoxText.Click
On Error Goto Errorhandler
Msgbox("ComboBox SelectedValue: " & Me.ComboBox1.SelectedValue)
Msgbox("ComboBox Text: " & Me.ComboBox1.Text)
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnSetBlankComboBox_Click(sender As Object, e As EventArgs) Handles btnSetBlankComboBox.Click
On Error Goto Errorhandler
Me.ComboBox1.SelectedIndex = -1
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnIsComboBoxEmpty_Click(sender As Object, e As EventArgs) Handles btnIsComboBoxEmpty.Click
On Error Goto Errorhandler
If Me.ComboBox1.SelectedIndex = -1 Then
Msgbox("ComboBox1 is empty.")
Else
Msgbox("ComboBox1 is not empty.")
End If
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
Private Sub btnIsComboBoxHyundai_Click(sender As Object, e As EventArgs) Handles btnIsComboBoxHyundai.Click
On Error Goto Errorhandler
If Me.ComboBox1.Text = "Hyundai" Then
Msgbox("The selected ComboBox text is Hyundai.")
Else
Msgbox("The selected ComboBox text is not Hyundai.")
End If
Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub
End Class