Link to home
Start Free TrialLog in
Avatar of Wease7
Wease7

asked on

Listbox Problem: Why does my ListBox reset the values on a TabControl SelectedIndex Change?

I've got a Form with a TabControl.  On the second tab, I have a ListBox with the selection mode set to MultiSimple.  When I load the form, the SelectedIndexChanged event of the Tab Control gets called before the Form Load does.  I have no idea why that is.  I load up my values from my DB in the Form Load event, but when I select the second tab, the values of that ListBox and what is supposed to be selected are lost.  It goes back to selecting the first value, which it does by default.  Again, I have no idea why.  So to fix that, I ended up loading up the values of that ListBox from the DB on that SelectedIndexChanged event of the Tab Control.  Which works fine (so everytime that specific tab is selected, it loads the correct values), but if someone changes the values and then goes to a different tab, once they go back to that tab, all of those values they just selected are gone.  Of course, since my SelectedIndexChanged event was tripped again and it reloaded the values from the DB.

Is this a bug in the .NET Framework?  Or am I doing something wrong?  None of the other controls act this way.  They all load up fine and don't change their values based on a Tab Control SelectedIndexChanged event.  What am I doing wrong?  Is there something I'm not setting somewhere?

If this truly is a bug, how can I get around this?  I want the values to load up properly and also keep them selected in that Listbox if the tabs change.  Any ideas?
Avatar of Vaxman2
Vaxman2

This event didn't fire while loading a form when I tested in VB2005, however it did happen with VB2003. However, in my test the items in the listbox stayed selected.

My fix would be to remove the Handles clause from the TabControl SelectedIndexChanged sub and to manually attach the handler at the end of your form_load routine.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("First selection")
        ListBox1.Items.Add("Second selection")
        ListBox1.Items.Add("Third selection")
        ListBox1.Items.Add("Fourth selection")
        ListBox1.SelectedItem = "Second selection"
        ListBox1.SelectedItem = "Fourth selection"
        AddHandler TabControl1.SelectedIndexChanged, AddressOf TabControl1_SelectedIndexChanged
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Debug.WriteLine("TabControl1 SelectedIndexChanged fired")
    End Sub


ASKER CERTIFIED SOLUTION
Avatar of ZeonFlash
ZeonFlash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wease7

ASKER

ZeonFlash:

Would you mind giving me some sample code for the array list backup and re-populate?

I'm going to upgrade to VS 2005 very soon, I think.
Private alListSelections As New ArrayList

''''To save the selections''''
'Clear the arraylist (of any previous saved selections)
alListSelections.Clear()

'Copy the selected items the array list
alListSelections.AddRange(ListBox1.SelectedIndices)

''''To load the selections back into the listbox''''
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged  'You won't need this line if you don't catch this event

'Clear the selected items and select the previously selected items
'You need to call this twice because of yet another bug w/ Listboxes.  If you only call it once,
'then the first item in the listbox will still be selected!
ListBox1.ClearSelected()
ListBox1.ClearSelected()

For i = 0 To alListSelections.Count - 1
      ListBox1.SetSelected(alListSelections(i), True)
Next

AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
Avatar of Wease7

ASKER

Thanks, ZeonFlash.  That did it.  What a pain in the neck!  I'm glad it just wasn't me that was crazy and there actually was a defect in there.