Hide Tab

triplemaya

Member
Joined
Aug 27, 2006
Messages
15
Programming Experience
Beginner
Hi. I have an app which puts up seven tabs in a basic window, and I need to get rid of two of them. If I delete them, I get a massive list of compile errors - I assume this is because of all of the things on that tab which no longer have a home. The obvious thing to do seems to be to hide these two tabs, but I can find no way to do this. There doesn't seem to be any property to do this with, which I would expect, coming from JAVA. I'd be very grateful if someone can tell me how to do this. I'm totally new to VB, I have been handed a project mostly finished, and I am supposed to deliver a finished product yesterday! I could wade through all the errors deleting all of the objects on these tabs, but I am concerned that this will produce further errors, and it's also going to be very sad indeed if I then need to put it all back!

I have found the post called Tabcontrol1.SelectedTab close?, and I have tried out the code, but it gives me the error "Reference to a non-shared member requires an object reference". I know I ought to know how to fix this, but I can't make it work. I have been to the microsoft reference page on this subject, where it says
  1. Declare the instance as an object variable.
  2. Reference the instance by the variable name.
but I don't know how to do these things.

Please could someone tell me what to type in.

the offending code is


Private TB as tabpage
and

TB = tabcontrol.TabPages(index of the tabpage you want to 'hide')
Tabcontrol.Tabpages.Remove(Your TabPage you want to 'hide')
I think that the tabpage I want to hide is TabControl7, that's the name in the properties box, but I'm not sure what to put in this space. When I type in TabControl7, it tells me that Name TabControl7 is not declared. Nor can I find out the index of this tabpage, unless it is 7. I am not used to working so far from the code and I'm totally lost.

also, when I type in
tabcontrol.TabPages( ...
it changes it to

Tabcontrol.TabPages( ...

which seems like it is likely to cause problems.


Please help.
 
TabControl7 is probably one of your TabControl instances. The tabpages is named TabPage1 2 3 etc if you haven't renamed them. I doubt you have renamed your tabcontrol "Tabcontrol" in which case you should name it something else than the class type name, the error you get also confirms that you haven't done so, so when you call "Tabcontrol.tabpages..." you in effect try to access the collection on the class type without an actual reference to an instance (like the one named "TabControl7").

When you click the control in Designer view you see what is choosen in the properties pane, click the tabcontrol and it reads "TabControl7" this includes clicking the tabs at the top - the tabs are managed by the tabcontrol not the tabpage. Click the tabpage blank area and top of properties pane read "TabPage1". You can also here select the control in the combobox list without clicking directly on the requested control on forms.

About hiding tabpages it can't be done. You can if you don't want to remove it completely just move it temporarily to your own collection/array, then move it back to the tabcontrol when needed. Example, my tabcontrol is here "TabControl1", there are some pages on it and I just remove/re-add first page with two buttons so you see the operation:
VB.NET:
Dim tpc As New List(Of TabPage)
 
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnRemove.Click
   tpc.Add(TabControl1.TabPages(0))
   TabControl1.TabPages.RemoveAt(0)
End Sub
 
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnAdd.Click
   TabControl1.TabPages.Insert(0, tpc(0))
   tpc.RemoveAt(0)
End Sub
Alternatively you can leave all the tabpages and work with the TabControls.SelectedIndexChanged event where you just prevent selection of some indexes (ie if it changes into an illegal index change it to some other).
 
continued

Many thanks for that JohnH.

You certainly seem to be right that none of the remove stuff works, which is odd seeing as how there is a whole MS page dedicated to that!
http://msdn2.microsoft.com/en-us/library/zb7xae05.aspx
Someone else whom I asked for help directed me to that. If it is totally documented, why doesn't it work? What's that all about?

Aaaaaanyway. I would be only too glad to use your code, for which I am truly grateful. It does produce some compilation errors which are confusing. I'd be very grateful indeed if you could point me at how to resolve them.

TabControl1.TabPages.Insert
'Insert' is not a member of System.Windows.Forms.TabControl.TabPageCollection

List
Type List is not defined

TabPage
Comma, ), or a valid expression continuation expected

btnRemove
Handles clause requires a WithEvents variable

btnAdd
Handles clause requires a WithEvents variable


Your last suggestion, leaving all the tab pages and changing the access, is not really an option as the whole point of this exercise is to do away with tabs which are not required.
 
If Type List is not defined, then that means you must be using .NET 1.1 (VS 2003) .... which means the above code will not work.... In the future, if the version of VS you are asking about is not the same as your designated primary platform (which is set to VS2005) .... you might want to mention that so the appropriate solution can be provided.

-tg
 
Yeah, removing tabs works fine (I said HIDE can't be done, not that remove doesn't work! Perhaps just a hack of terminology, but there are things that specifically can HIDE in VB.Net.), My code is platform .Net 2.0 same as yours triplemaya when posted, maybe you change this now in your user profile. I haven't got older .Net code right now, I will see tomorrow if noone else posts. Maybe you figure it out yourself from the basic operations done, you can use an ArrayList or strong type Array for example in older .Net instead of the List(Of TabPage), or even just two variables of type TabPage if there is only two tabpages you want to remove/re-add sometimes.
 
YEah, I went around and around with the TabStrip control recently... ticked me off that I couldn't simply hide tabs.... which incidently MS recommends (for the upcomming Aero interface design) when creating options forms with tabs... if it doesn't apply to the situation, it shouldn't be shown.... then they show their very own MS Word options form as a BAD example.... maybe it's because their own tab control is so mucked up that pages can't be hidden.... we can only hope they fix this in FW3.
 
Version idiocy!

Thanks guys. I was originally on 2005, but I switched back to 1.1 when I found that the code I compiled under VB Express 2005 wouldn't run on a standard 2.0 installation, but demanded V2.0.50727. Then I discovered that I had been using an old beta dotnetfx as my 'standard' 2.0. The usual tale of confusion by the inexpert!

At this point going back to 2005 looks smart, especially if I can just remove the tabs programatically. My only concern is that there might be a significant number of users who have an earlier version of .net, but I guess that's unlikely. I've googled about but I can't find any figures for this. If anyone has any info about this that would be very helpful.
 
What are you on about, of cource you can remove tabpages in .Net 1, use the Remove method or the RemoveAt method. To add it again you must use Add method, since Insert method don't exist in old .Net version. When you use Add method it is appended, to so locate it again you use the IndexOf method. Anyway .Net 2.0 is recommended.
 
But, but, ...?

I have this in my code

TabControl1.TabPages.Remove(TabPage5)

all compiled successfully and everything, and it doesn't work. I assumed it was because it was an early version, since I was told that this
TabControl1.SelectedTab.Hide()
was simpler, but didn't work in early versions, and since the above code didn't work I assumed that applied to this also.

Interval while I go off to check code

Doh
It wasn't getting called!

Many thanks for your persistence!
 
I'm having some weird results with this code. I have a tabcontrol with 6 tabs. I'm trying to "hide" the last 2 tabs. I have this code in a module:

VB.NET:
    Dim tpc As New List(Of TabPage)

    Public Sub RemoveTabs(ByVal intIndex As Integer)
        tpc.Add(frmMain.TabControl1.TabPages(intIndex))
        frmMain.TabControl1.TabPages.RemoveAt(intIndex)
    End Sub

    Public Sub AddTabs(ByVal intIndex As Integer)
        frmMain.TabControl1.TabPages.Insert(intIndex, tpc(0))
        tpc.RemoveAt(0)
    End Sub

Then from frmMain I have 2 buttons:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RemoveTabs(4)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        AddTabs(4)
    End Sub

When I run that, sometimes it deletes the correct tab and upon re-adding, it adds the name of the tab next to it instead of the original tab name, and the web browser controls on the last two tabs are no longer there. Just blank tabs.

And sometimes, it deletes the tab in the 1 slot (tab index) for no apparent reason.

Can anyone point out what I'm doing wrong?

thanks :)
 
Dear GeekDrop:

After generisizing(if that's a word) the code above, I successfully tested the code below in FW 4.0 which corrects what I think is a problem of scope solved by putting the List declaration at the top of the screen and adding it to the parameter list fro the module subroutines.

In the top of the screen:
Dim tpc as New List(OfTabPage)

In a module:
Sub
Remove_Tab(ByVal tpList As List(Of TabPage), ByVal tabTabControl As TabControl, ByVal intTabIndex As Integer)
tpList.Add(tabTabControl.TabPages(intTabIndex))
tabTabControl.TabPages.RemoveAt(intTabIndex)
End Sub


PublicSub Add_Tab(ByVal tpList As List(Of TabPage), ByVal tabTabControl As TabControl, ByVal intTabIndex As Integer)
tabTabControl.TabPages.Insert(intTabIndex, tpList(0))
tpList.RemoveAt(0)
End Sub


Hope this helps.
 
Last edited:
Continued:

Sorry for the funky formatting(it's my web browser) and the inconsistent variable names but the code works.

Called in the screen as:
Add_Tab(tpc,Me.TabControl1,4)

and

Remove_Tab(tpc,Me.TabControl1,4)
 
Last edited:
Back
Top