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

SheritlwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JackOfPHCommented:
Try this code...


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(Tabs.tag, tab)
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JackOfPHCommented:
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

0
SheritlwAuthor Commented:
tab.index is not an option for a tabpage.
thanks
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

JackOfPHCommented:
'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

0
JackOfPHCommented:
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

0
SheritlwAuthor Commented:
That looks good, how would I show the tab now?
Thanks
0
JackOfPHCommented:
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

0
SheritlwAuthor Commented:
It still appends the tab to the end of the tabcontrol.
thanks
0
SheritlwAuthor Commented:
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
       
0
JackOfPHCommented:
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
       
0
JackOfPHCommented:
0
Hawkvalley1Commented:
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?
0
SheritlwAuthor Commented:
Yes, I see the flaw.  Basically I need to move the added tab over one position
0
Hawkvalley1Commented:
Yes, I told you how to do that:
tabcontrol1.tabpages.insert(tabcontrol1.tabpages.count - 2, tab)

insert(<index>, <tabpage>)
0
Hawkvalley1Commented:
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?
0
SheritlwAuthor Commented:
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,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.