After browsing around a little bit, I found an interesting solution if you need to hide the tab headers on a tab control. I found it here, posted on the MSDN social forums by “nobugz” (Hans Passant). Here is his solution:
class YourTabControl : TabControl{ protected override void WndProc(ref Message m){ // Hide tabs by trapping the TCM_ADJUSTRECT message if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1; else base.WndProc(ref m); } }
Basically, you define your own class that extends TabControl. The custom class overrides the WndProc
method, which is responsible for processing Windows messages sent to windows and controls. The new version filters out the message which is responsible for drawing the tab headers. Very clever.
To use it, you can make a normal TabControl using the designer. Then, in the Designer code, change the following line (in the InitializeComponent
method):
this.tabControl1 = new System.Windows.Forms.TabControl();
to
this.tabControl1 = new YourTabControl();
Finally, near the bottom of the Designer code should be a line that looks like this:
private System.Windows.Forms.TabControl tabControl1;
Change it to:
private YourTabControl tabControl1;
Another clever thing about this is that, as you will notice, tab headers are still visible while in Design mode.
Thanx! Works fine.
Thank you!
I had found the code sample, but you have to do this manipulation
to get it working!
I implemented this, and I see that the Tabs are an all-or-nothing visible or not currently;
Is it possible to extend the class so that individual tabs are visible or not?
Hi sqlstorm – Unfortunately I don’t think there is a way with the current code. You might look into finding a third-party tab control.
Is there a way to manipulate this code in order to show and hide the tabs at runtime, such as by clicking a button?
I think so. You should be able to add a property to the class to disable/enable the code in the WndProc method.
Thanks, a simple boolean toggle works, however it wont redraw the tabs immediately. I have to switch hidden tabs programmatically and then they display. Ive tried .Refresh(), .Invalidate() and .Update() but none seem to force an instant redraw.
If an immediate redraw isn’t possible I’ll just do a work around for a quick switch and switch back between tabs.
Hmm, that’s strange. Are you calling Refresh/Invalidate on the individual tabs or on the tab control? Another thing to try would be to invalidate the entire Form.
I tried both, the TabControl and the whole Form, neither caused the tabs to be redisplayed immediately, although it still appeared to be redrawing (background image flickered, things like that). Tabs still would not redisplay until they were switched.