Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

Restore hidden tabpage to original position when stored in dictionary

I am using the code below to hide and show tabs.  The tab names are stored in a dictionary
The code I am currently using works great if you just need to append the shown tab to the end of the tabcontrol, but not when you need to show it in it's original location before hidden.
I tried using insert, but it does not restore the correct tabpage.  Each tab pages controls are numbered according to the tabs index, so if a using wants to show a tab, they select the tab they want to display and I need to display that tab.
It works if I just add the tab, but if I insert the tab, I get a different tab.
Basically I need to append the tab to the 2nd to last position in the tabControl.
i.e. position = tabcontrol1.tabcount - 2
I hope this is clear.
Anyone have any ideas?
Thanks
Tab 0 =  controlname & 0
so the name would be controlname0
Tab 1 =  controlname & 1
so the name would be controlname1

etc.
Dim tabs As New System.Collection.Generic.Dictionary(Of String, TabPage)
 
Private Sub RemoveTab(name As String)
  Dim tab As TabPage = TabRacing.TabPages(name)
 
  tabs.Add(name, tab)
  TabRacing.TabPages.Remove(tab)
End Sub
 
Private Sub RetrieveTab(name As String)
  Dim tab As TabPage = tabs(name)
  tabs.Remove(tab)
  TabRacing.TabPages.Add(tab)
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

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
opps.. There is a slight problem to the code I posted above so try this one instead...
Private Sub RemoveTab(name As String)
  Dim tab As TabPage = TabRacing.TabPages(name)
 
  tabs.Add(name, tab)
  tab.tag = tab.index  '<----  This will store the original index location of the tab in the tag property.
  TabRacing.TabPages.Remove(tab)
End Sub
 
 Private Sub RetrieveTab(name As String)
  Dim tab As TabPage = tabs(name)
  tabs.Remove(tab)
  TabRacing.TabPages.Insert(tab.tag, tab)
End Sub

Open in new window

Avatar of Sheritlw

ASKER

tab.index is not an option for a tabpage.
thanks
'Replace your code to this...

Private Sub RemoveTab(name As String)
  Dim tab As TabPage = TabRacing.TabPages(name)
 
  tabs.Add(name, tab)
  tab.tag = GetIndex(name)  '<----  This will store the original index location of the tab in the tag property.
  TabRacing.TabPages.Remove(tab)
End Sub

Then add this code.


Private Function GetIndex(Byval Tabname as string) as integer
 
        For ctr As Integer = 0 To TabControl1.TabPages.Count - 1
            
            If TabControl1.TabPages(ctr).Name = Name Then
                Return ctr
            End If
 
        Next
 
        Return -1
 
End Function

Open in new window

oopps... typing error...

Add this code instead
Private Function GetIndex(Byval Tabname as string) as integer
 
        For ctr As Integer = 0 To TabControl1.TabPages.Count - 1
            
            If TabControl1.TabPages(ctr).Name = TabName Then
                Return ctr
            End If
 
        Next
 
        Return -1
 
End Function

Open in new window

That looks good, how would I show the tab now?
Thanks
Just use this code....

 Private Sub RetrieveTab(name As String)
  Dim tab As TabPage = tabs(name)
  tabs.Remove(tab)
  TabRacing.TabPages.Insert(tab.tag, tab)
End Sub


Below is the complete and working code...
Private Sub RemoveTab(name As String)
  Dim tab As TabPage = TabRacing.TabPages(name)
 
  tabs.Add(name, tab)
  tab.tag = GetIndex(name)  '<----  This will store the original index location of the tab in the tag property.
  TabRacing.TabPages.Remove(tab)
End Sub
 
 Private Sub RetrieveTab(name As String)
  Dim tab As TabPage = tabs(name)
  tabs.Remove(tab)
  TabRacing.TabPages.Insert(tab.tag, tab)
End Sub
 
Private Function GetIndex(Byval Tabname as string) as integer
 
        For ctr As Integer = 0 To TabControl1.TabPages.Count - 1
            
            If TabControl1.TabPages(ctr).Name = TabName Then
                Return ctr
            End If
 
        Next
 
        Return -1
 
End Function

Open in new window

It still appends the tab to the end of the tabcontrol.
thanks
If it would be easier.  Just as long as the tab is shown before the last tab.
When I hide the tabs I use the following...
 
For p As Integer = ft.TabRacing.TabCount - 2 To iMaxTabs Step -1

                ft.HideTab(ft.TabRacing.TabPages(p).Name)
 Next
       
What does this code do? Can you explain?

For p As Integer = ft.TabRacing.TabCount - 2 To iMaxTabs Step -1

                ft.HideTab(ft.TabRacing.TabPages(p).Name)
 Next
       
Small flaw in your plan Sheritlw, if you try to store the index of the tabpage and do some adding and removing how will an index of 5 work if they now have only 2 pages showing?
Yes, I see the flaw.  Basically I need to move the added tab over one position
SOLUTION
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
If your adding controls based on the index, this is the wrong approach. You could try to store the number in the tag property. This way as you add/remove controls it will not change the tag and it will load the right controls for you. Is this more like what you are doing?
Well it looks like I got it working.  
A page with grids already filled in would appear, so I basically had to reset everything so it would then be a blank and new tab, ready for data entry.
The show is as follows...
Private Function ShowTab(ByVal sName As String) As TabPage
        Dim tab As TabPage = tabs(sName)

        TabRacing.TabPages.Insert(Me.TabRacing.TabCount - 1, tab)
        tabs.Remove(sName)

        Return tab

    End Function

Since I used info from both reply's, I will split the points.


Thanks for your help,