Solved

How to properly work with ComboBoxes?

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

Technology Partners: 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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