Removing tabs from tab control.

you can clear the tabs...
tabMain.Controls.Clear()

or

remove only one or more...

tabMain.Controls.Remove(tabSecond)
tabMain.Controls.Remove(tabFifth)
 
Sorry, didn't explain myself clearly :)

What I want to do is keep all the tab sheets but get rid of some of the little tabs above them. That way I can control what the user can interact with. For example i might not want them to be able to see certain tabs if they don't have admin privilages.
 
you can do something like this...
Visual Basic:
    Private Sub ChangeControlsState(ByVal bState As Boolean) 
        Try
            Dim ctr As Control
            For Each ctr In tabClient.Controls
                If TypeOf ctr Is TextBox Then
                    ctr.Enabled = bState
                End If
            Next

            For Each ctr In tabLoan.Controls
                If TypeOf ctr Is TextBox Or TypeOf ctr Is ComboBox Or TypeOf ctr Is NumericUpDown Then
                    ctr.Enabled = bState
                End If
            Next
            btnApply.Enabled = bState
        Catch 'ignore errors
        End Try
    End Sub
 
I think Robby is enabling/disabling controls, which isn't quite what RichDay was looking for.

Unfortunately, you can't disable a TabPage. You also can't set it to Visible=False. You have to actually remove it from the collection to which it belongs - the TabControl itself. Assuming you're defining your tabs in the Designer, your variable for each TabPage will exist Form-wide. Meaning, if you have code that needs to remove the tab and later add it, you'll always have a reference to the TabPage. This only matters if you had planned on creating your tabs in code using local variables rather than form-level variables.

Here's some code:
Code:
' Remove a tabpage named TabPage2 from the
' tab control TabControl1
TabControl1.Controls.Remove(TabPage2)

'Here's putting it back
TabControl1.Controls.Add(TabPage2)

Unfortunately, I don't know how to get the tab put back in any particular place. I've only had to do this once and there were only 3 tabs, so I did a Clear and then added them back in the order I wanted.

-Nerseus
 
It's not unfortunate, it's just not the way the thing is supposed to work. Nowhere in Windows or any other app have I seen people either disabling tabs, or removing individual ones.

The tabbed dialog control is supposed to group together similar sets of controls, where they wouldn't all fit on to one form. Nothing more, nothing less. Bearing that in mind, you might want to re-think how your interface works.
 
Removing tabs is fairly common - you just may not realize it's being done, depending on who you are. For instance, a file's property dialog box (right click any file in explorer and hit properties) may not show certain tabs like Sharing or Security if you don't have them enabled in your policy. I'm sure MS didn't write multiple versions of the dialog - they probably just removed the tabs that you didn't have access to.

As for disabling, I've never seen it done that I can think of. I did have one client that really wanted to have a tab look disabled. Luckily we talked them out of it. But some people have super-smart clients that want things done their way, regardless of standards. That, like not being able to disable a tab, is unfortunate :)

-Nerseus
 
What you can do is create the tabs at run-time, and only
create the needed controls when required. (user level and such)

The two methods I used above (removing tabs and disabling
controls) would most likely be used if the controls were all
created at design-time.
 
Nerseus - those dialogs never have their tabs removed. The api tabbed dialog is such that I would bet money that those tabs are just never added in the first place.
 
I would create them dynamically but I have quite a lot of controls to put on the pages in a fairly complex layout so I'd rather do it in the designer.

Thanks for the pointer Nerseus, to get around it I might remove all of the tabs from the collection when the app starts and then add only the ones I want as required.

[edit]
Ok, that seems to work just fine, thanks very much. :)

One real life example of this: My app has a config form split up into sections (input/output files, storage directories etc...) with a tab control. I want developers for the interfaces my app exposes to have access to the raw config and setup files as well as the graphical config normal users see. It doesn't make much sense to hide this away in a separate window as the changes they make to the setup file impact on what is seen in the config window. This way they can save their changes and immediately see their effects.
[/edit]
 
Last edited:
Howdy partner!

I thought you were being clever with you new dot net code. Now I know where you get your ideas.

:D :D

Catch Ya later mate.
 
You might be right, divil, but I like designing my tabs with Visual Studio. If you do all the design work by hand... well, it's tedious - unless you know another way than manually finding the right x/y coordinates for each control :)

About six months ago, I played around with trying to develop TabPages through a separate .cs file (inheriting from TabControl) but could never really get anything to work well. Ideally, a TabPage would be similar to a user control or a form and they could be designed completely independently of the TabControl they were contained in. But since .NET doesn't give us that, removing them from the collection is the best bet.

If you don't want to go through the trouble of creating the controls, you could design with the TabPages all added, then move out all of the code from InitializeComponent to a separate function before switching to Release mode. It's a bit more work to save a bit during runtime, but doable.

- Nerseus
 
I had the same problem and found that you can't hide the tabs of a standard Windows tab control and just keep the panels. Someone pointed me to a free .net tab control at www.dotnetmagic.com. This is a very nice tab control that enables you to turn off the actual tabs (the little part that sticks up above the rest of the tab page) but keep the tab page itself. You can use each tab page of the control as a container for other objects, and switch between pages programatically. I have used this approach to create an Outlook-style UI. The main part of the screen is a dotnetmagic tab control without any tabs showing. I have an Outlook bar on the left, so when the user clicks various icons in the Outlook bar, it changes the active page of the tab control. It is way cool. Only complaint I have about this control is that if you do want to show the tabs, you can only have them at the top or the bottom of the control. You can't have them aligned vertically on either the left or right side of the control. For this, you have to use a standard Windows tab control.
 
Why not just use Divil's suggestion of multiple Panel controls and hide/show each one as needed? Carrying around a whole separate control is bound to take up more resources and means another file (at least one) to install. The only real disadvantage to using Panels is keeping them organized at design time and that can be overcome if you're careful in how you name and arrange them.

Another alternative, one I haven't used since VB3 so I don't know if it works, is to use the standard tab control but set it's top property (or it's location now) to something like -20 -- just enough so that the tabs don't show up but the whole tabpage does. If you're going to use a Tab Control of some sort, this may work better than the DotNetMagic version. Nothing against them (never seen it) but if you don't have to use a custom control it's generally better not to.

-Nerseus
 
So does tabMain.Controls.Clear() clear whole tab page or just the little top tab???

Another way to hide little tabs it to put tabcontrol in panel and move it to top to hide little tabs (so you can have it i the middle of the page)

And yes I can do that with hiding panels but its a nightmare in designer (especially if you have more than 10 panels :) )

Igor
 
What can I say, sometimes you have to leave the comfort of the designers and actually write some code :P :)
 
Back
Top