Solved

How to properly work with ComboBoxes?

Posted on 2014-02-20
2
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

27 Experts available now in Live!

Get 1:1 Help Now