3

Ive seen some discussion on here about how to hide tabs in a tabcontrol but they all seem to be in C or some variant. I havent seen one for vb.net (i cant do C)

What i want to do is hide or disable all some of the tabs till the user has logged in. Ive sorted out the login and logout. All i need to do is add the code to enable/disable some tabs until the user has logged in.

Anyone know a good way to do this?

WinForms btw

Dr.Pepper
  • 559
  • 4
  • 11
  • 27

3 Answers3

9

You just add and remove TabPages from the TabControl through the TabPages collection:

TabControl1.TabPages.Add(myTabPage)

and to remove it:

TabControl1.TabPages.Remove(myTabPage)

Note: Removing a TabPage does not dispose it, it just removes it from the TabPage collection.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • doesnt that method loose all the controls that are on the tab though? – Dr.Pepper Jan 29 '12 at 18:58
  • 1
    @user977229 No. The `TabPage` will still have all of its controls and events until it gets disposed. – LarsTech Jan 29 '12 at 19:02
  • One problem with this method that you will have to account for is that when you attempt to add the tab back to the tab control, it will be added at the end, not in its original position. You may wish to keep track of where you had the tab page and use the InsertAt method to insert it in the correct location. – Chris Dunaway Jan 30 '12 at 15:04
  • @ChrisDunaway True, but in the OP's case, it sounded like one tab for the login, and if successful, he was just going to remove the one tab and put in the rest of the application's tab. It would have been nice if Microsoft would have included a `Visible` property for the TagPage. – LarsTech Jan 30 '12 at 15:11
  • @ChrisDunaway - in fact it doesnt insert it at the end. With the "add tabpages" command, the order in which you put them is what it ill come out as - for example, my order intentionally goes - (1,2,3,6,4) and if i put 5 "add commands" in and put the "6" above the "4" it inserts it correctly. No "insertat" needed. And yeah lars, wouldve been nice – Dr.Pepper Feb 02 '12 at 14:26
  • I'm not sure I follow you. The Add method of the TabPages will add the tab at the end. So in your example with tabs (1,2,3,6,4). If you remove tab 3 in order to hide it, if you then call Add later to add it back, it will be added at the end (1,2,6,4,3). I meant, in my comment, that you will have keep track of the proper location yourself. – Chris Dunaway Feb 02 '12 at 16:03
  • Ok, two years later. :) How did you manage to not loose the controls? I have a tab panel with a form, and i do not always want to show it. But when i go with this .Add /.Remove method i have to programatically set up the form in the tab layout. If i dispose it, and then adds it (a new) it will of course be blank? – gubbfett Jun 23 '14 at 11:52
  • @gubbfett Yes to `If i dispose it, and then adds it (a new) it will of course be blank?` If you remove TabPages via the designer, those pages will get disposed. The Add/Remove method in my post is only for runtime. I do not understand this: `But when i go with this .Add /.Remove method i have to programatically set up the form in the tab layout.` – LarsTech Jun 23 '14 at 15:27
  • Well, if i make 3 tabs and put a button in each one of them, and then during runtime i remove the last one, and then adds it with a new one it will be blank - the button is gone. In the answer code above, the myTabPage holds the layout? Where does it come from? Since i make the design in the design view, i will need to save this tab (as myTabPage, maybe) for future use before i remove it, i suppose? Or make myTabPage layout programatically during runtime? – gubbfett Jun 24 '14 at 09:19
  • @gubbfett Why don't you just try it? Create the TabPage in the designer, remove it at runtime, add it back in when you want to see it. You might have to hit the "Ask Question" link above if you get stuck somewhere. It's not clear to me where you are stuck since I can't see your code. – LarsTech Jun 24 '14 at 13:28
1

Currently, the following code block disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.

TabMyTab.Enabled = False

If you want to disable the tab similar to i.e. button.Enabled = False which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:

Private PreviousTab As New TabPage
Private CurrentTab As New TabPage

Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
        PreviousTab = e.TabPage
End Sub

Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
        CurrentTab = e.TabPage
        If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
            MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            TabControlName.SelectedTab = PreviousTab
        End If
End Sub

Substitute your own values for "UnselectableTab" and "TabControlName" for your project.

j3josh6
  • 51
  • 4
0

Just hide the whole TabControl by setting its Visible property

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Sorry - forgot to mention that the login is the first tab. I know its a messy way to do it and i will probably change it but id also like to learn how to do this for any future projects – Dr.Pepper Jan 29 '12 at 18:52