I have this code behind a winforms which simply has a listbox as its only control:
Imports System.Windows.Forms
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
ListBoxX.Items.Add("hello")
ListBoxX.Items.Add("world")
ListBoxX.Items.Add("foo")
ListBoxX.Items.Add("bar")
End Sub
Private Sub ListBoxX_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBoxX.SelectedIndexChanged
MessageBox.Show("change registered")
End Sub
Private Sub ListBoxX_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles ListBoxX.SelectedValueChanged
MessageBox.Show("change registered X")
End Sub
End Class
If “hello” is selected and I then click on “hello” then the message box appears – surely if I’m clicking on the same item then the SelectedIndex has not changed – so why is this event firing? How do I ensure it only fires when the index changes?
Check inside the event if it is the same item as selected before,
Save the last selected item each on click.
It’s been a while since I coded in VB, so I hope this code is OK.
I assume that the event will always fires, no matter which item is selected.
Another solution is to override the ListBox User Control, but I think you don;t need that.