devonmanelski.com
 
Home | I.T. Management | Business Analysis & Project Management | VB.Net | SQL Server | About |

The ComboBox Control
  1. Summary
  2. The Items Collection of a ComboBox is the simplest way to populate a ComboBox with values. However, when building most software applications you will find that it is more logical to use a numeric value for each of the text values in the ComboBox. This is even more so true when building a database driven application that is based on a relational database.

  3. Screenshots
  4. Sample Code
    1. Data
      1. Items Collection
      2. 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

      3. Class Driven
      4. 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.

        1. Write a Class that has a Property for the ID and a Property for the Text Value.
        2. Write a routine to initialize the Property Values of the Class.
        3. Create an instance of the Class for each Value in the ComboBox.
        4. Add each instance of the Class to a List that is of the Type of the Class.
        5. Bind the List to the Data Source Property of the Class.
        6. Set the DisplayMember Property of the ComboBox to the name of the Class Property that holds the Text Value.
        7. 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

    2. AutoComplete
      1. AutoCompleteMode - Suggest
      2. AutoCompleteSource - ListItems
    3. Events
      1. SelectionChangeCommitted - The SelectionChangeCommitted event is used to capture the event that is fired when a user manually chooses a new option from a ComboBox.
      2. SelectedIndexChanged - The SelectedIndexChanged event is used to capture the event that is fired when a user manually or programatically chooses a new option from a ComboBox.
      3. SelectedValueChanged - The SelectedValueChanged event is used to capture the event that is fired when a user manually or programatically chooses a new option from a ComboBox.


Copyright © Devon Manelski