Question

Looking for some insight here...

On my form, I have three ComboBox controls. When the left most receives a selection, it calls a routine to enable and populate the 2nd, then the same happens for the 3rd when a selection is made in the 2nd.

    Private Sub cboEquipment_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboEquipment.SelectedIndexChanged

    Me.transData.isMobile = If(Me.cboEquipment.Text = MOBIEQIP, 1, 0)

    If cboMembership.Enabled = False Then

        Me.cboMembership.Enabled = True

    End If

    Call loadGroups()

End Sub

The method 'loadGroups'

    Private Sub loadGroups()

    Dim cn As New SqlConnection(My.Settings.MyConnectionString)
    Dim cm As New SqlCommand("SELECT ... ORDER BY codeHumanName ASC", cn)
    Dim ta As New SqlDataAdapter(cm)
    Dim dt As New DataTable

    Me.cboMembership.DataSource = Nothing
    Me.cboMembership.Items.Clear()

    Me.transData.membership = Nothing

    cn.Open()
    ta.Fill(dt)

    With Me.cboMembership

        .DataSource = dt
        .DisplayMember = "codeHumanName"
        .ValueMember = "codeID"

    End With

    cn.Close()

End Sub

This works fine an dandy, everything loads at should, and provides the appropriate .ValueMember results.

I should mention that when the Form first loads, boxes 2 and 3 are empty and disabled. After their initial load, I have to select an item from the list to get values (as expected).

The odd part is that once the 2nd or 3rd Combobox have been 'initialized', they seem to recall last settings. For example, if I call the Reset method, which sets the .DataSource property to Nothing, clear the list, and set the Text to Nothing; the next time I change the SelectedIndex on cboEquipment, the other boxes come back to life with SelectedItems and SelectedValues.

These values aren't wrong according to the new DataTable, but it is odd that they remember at all (they shouldn't have any reference, and behave as a fresh load).

Other points of interest; the populate methods are called during the SelectedIndexChanged event.

Any thoughts would be great!


UPDATE -

This behavior is tied in with the SelectedIndexChange Event bouncing around (raise! raise! raise!)

Removing and adding event handlers at strategic points has my code working as I want/expect it to.

Here is the article that put me on the right path...

Was it helpful?

Solution 2

I ended up using AddHandler and RemoveHandler to prevent chain-firing events as the comboboxes were being populated.

OTHER TIPS

On your second and third combo boxes try clearing the .Text/.SelectedText properties before you set the .DataTable

The combo box is probably just being too helpful and assuming that when you set the .DataTable a second time, you'd want the selected item to match your .Text property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top