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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.S electedInd ices)
''''To load the selections back into the listbox''''
RemoveHandler ListBox1.SelectedIndexChan ged, AddressOf ListBox1_SelectedIndexChan ged '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(alLis tSelection s(i), True)
Next
AddHandler ListBox1.SelectedIndexChan ged, AddressOf ListBox1_SelectedIndexChan ged
''''To save the selections''''
'Clear the arraylist (of any previous saved selections)
alListSelections.Clear()
'Copy the selected items the array list
alListSelections.AddRange(
''''To load the selections back into the listbox''''
RemoveHandler ListBox1.SelectedIndexChan
'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(alLis
Next
AddHandler ListBox1.SelectedIndexChan
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.
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
ListBox1.Items.Add("Third selection")
ListBox1.Items.Add("Fourth
ListBox1.SelectedItem = "Second selection"
ListBox1.SelectedItem = "Fourth selection"
AddHandler TabControl1.SelectedIndexC
End Sub
Private Sub TabControl1_SelectedIndexC
Debug.WriteLine("TabContro
End Sub