?
Solved

How to properly work with ComboBoxes?

Posted on 2014-02-20
2
Medium Priority
?
226 Views
Last Modified: 2014-02-20
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

0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39875289
Remove the Handles clause at the right of your event declaration. This will prevent the event from firing.

Load the ComboBox.

When you are ready to start reacting to events, add the following line in your code

AddHandler cmbEquipmentNames.SelectedIndexChanged, AddressOf cmbEquipmentNames_Manufacturer_SelectedIndexChanged

If you need to refresh the ComboBox contents while the application is running, call RemoveHandler with the same syntax, refresh and call AddHandler again.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39875342
That worked great James...Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question