Solved

How to properly work with ComboBoxes?

Posted on 2014-02-20
2
210 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
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now