Combobox SelectedValueChange is fireing when value don't change

kejpa

Junior Contributor
Joined
Oct 10, 2003
Messages
320
Hi,
I have a combobox that contains four items and it's located on a tab that isn't visible when I show the form. When the tab becomes active the SelectedValueChange event is fired once for each item

Code:
'FormLoad....
Dim oTraceList As New ArrayList()
    oTraceList.Add(New cComboItem(TraceLevel.Off, "Trace off"))
    oTraceList.Add(New cComboItem(TraceLevel.Info, "Info"))
    oTraceList.Add(New cComboItem(TraceLevel.Warning, "Warning"))
    oTraceList.Add(New cComboItem(TraceLevel.Error, "Error"))
    oTraceList.Add(New cComboItem(TraceLevel.Verbose, "Verbose"))

    cboTraceLevel.DataSource = oTraceList
    cboTraceLevel.DisplayMember = "Display"
    cboTraceLevel.ValueMember = "Value"

    cboTraceLevel.SelectedIndex = -1
    AddHandler cboTraceLevel.SelectedValueChanged, AddressOf cboTraceLevelChanged
    cboTraceLevel.SelectedIndex = 0

'FormLoad continues....

Private Sub cboTraceLevelChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Static iSelectedValue As Integer
    MsgBox("Old:" & iSelectedValue & vbNewLine & "New:" & cboTraceLevel.SelectedValue)
    iSelectedValue = cboTraceLevel.SelectedValue
End Sub

Public Class cComboItem
Private iValue As Long
Private sDisplay As String
Private iImageIndex As Integer = -1
Public Overrides Function ToString() As String
        Return sDisplay
End Function

Public Sub New(ByVal Value As Long, ByVal Display As String)
    iValue = Value
    sDisplay = Display
End Sub

Public ReadOnly Property Value() As Long
    Get
        Return iValue
    End Get
End Property
Public ReadOnly Property Display() As String
    Get
        Return sDisplay
    End Get
End Property
End Class

When I click on the selected value the event also fires.

This doesn't seem like it's supposed to work?
At least not according to my head ;)

/Kejpa
 
As you have found, every time an item is added to the combo box, the selected index change index is triggered...So the easy way around this is to have a Boolean variable named firstLoad declared in the form's declaration section and set it to True.

Dim firstLoad As Boolean = True

Then after everything is loaded from the Form Load event, set firstLoad to False.
In the combox box's SelectedIndexChanged sub use:
If Not firstLoad then....

OR if its possible
Put your selection events in the combo's click sub and not the selected index changed sub.
 
Last edited:
But. If you have look at my code you will notice that I'm not using the SelectedIndex even. I'm using the SelectedValue, the SelectedValue isn't changing when new Items are added, is it?

Another thing, I'm not using the "Handles", I'm adding the handler after the items are added to the Combo.
The event is fireing when I change to the tab where the Combo is located not when the items are loaded.
Also, the event is fireing when I drop down the list and select the same item.

Temporarily I'm solving this by a static variable in the event handler, but it's not the way I want it to be...

Regards
/Kejpa
 
I used to have the same problems with listviews, listboxes and comboboxes when first loading them. Changing to the control's click event solved these problems.
 
Back
Top