Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

How to properly work with ComboBoxes?

I know this seems like programming 101 but I always have issues with ComboBoxes and which events to use. My problem is that I have a combobox that gets preloaded with data when the form loads. I always initialize my ComboBox's SelectedIndex property to -1. However, as soon as the cmbEquipmentNames_Manufacturer has the DataTable assigned to it's DataSource, the SelectedIndexChanged Event fires. Is there a way to prevent this or what is the industry standard for dealing with ComboBoxes?

The blnIsLoading variable is a Boolean variable that prevents the SelectedIndexChanged Event from executing all the code while the Form is loading. Once the Form is loaded, this flag is set to False.

    Private Sub LoadEquipmentNames_ManufacturerComboBox()
        Try
            InitializeErrorClass(EH)
            BL.LoadEquipmentNames_ManufacturerComboBox(EH)

            If EH.DataSet.Tables("Temp").Rows.Count > 0 Then
                cmbEquipmentNames_Manufacturer.DataSource = Nothing
                cmbEquipmentNames_Manufacturer.DataSource = EH.DataSet.Tables("Temp")
                cmbEquipmentNames_Manufacturer.DisplayMember = EH.DataSet.Tables("Temp").Columns(1).ToString
                cmbEquipmentNames_Manufacturer.ValueMember = EH.DataSet.Tables("Temp").Columns(0).ToString
                cmbEquipmentNames_Manufacturer.SelectedIndex = -1
            End If

            EH.ErrorMessage = ""

        Catch ex As Exception
            EH.ErrorMessage = "LoadEquipmentNames_ManufacturerComboBox() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
   '
   '
   '
    Private Sub cmbEquipmentNames_Manufacturer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbEquipmentNames_Manufacturer.SelectedIndexChanged
        Try
            If Not blnIsLoading Then   
                If cmbEquipmentNames_Manufacturer.Text.Length > 0 Then
                    strEquipmentName_Manufacturer = cmbEquipmentNames_Manufacturer.Text
                    ConcatenateValues()
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "cmbEquipmentNames_Manufacturer_SelectedIndexChanged() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlakeMcKenna

ASKER

That worked great James...Thanks!