Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

	
'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

  • *Experts*
Posted (edited)

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.

Edited by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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

  • *Experts*
Posted
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.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...