Hide Tab Header on C# TabControl
I am developing a Windows Form Application with several pages. I am us开发者_JAVA百科ing a TabControl to implement this. Instead of using the header to switch between tabs, I want my application to control this e.g. the next tab should open after the user has filled in a text box and clicked the next button.
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. It shows the tabs at design time so you can easily switch between them while designing. They are hidden at runtime, use the SelectedTab or SelectedIndex property in your code to switch the page.
using System;
using System.Windows.Forms;
public class TablessControl : 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);
}
}
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
Create new UserControl, name it for example TabControlWithoutHeader and change inherited UserControl to TabControl and add some code. Result code should look like:
public partial class TabControlWithoutHeader: TabControl
{
public TabControlWithoutHeader()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x1328 && !DesignMode)
m.Result = (IntPtr)1;
else
base.WndProc(ref m);
}
}
After compile you will have TabControlWithoutHeader control in ToolBox. Drop it on form, in designer you will see headers, but at runtime they'll be hidden. If you want to hide them in designer too, then remove && !DesignMode
.
Hope that helps.
http://social.msdn.microsoft.com/Forums/windows/en-US/c290832f-3b84-4200-aa4a-7a5dc4b8b5bb/tabs-in-winform?forum=winforms
You can replace tabcontrol with a hand made panel that mimic like you want:
class MultiPagePanel : Panel
{
private int _currentPageIndex;
public int CurrentPageIndex
{
get { return _currentPageIndex; }
set
{
if (value >= 0 && value < Controls.Count)
{
Controls[value].BringToFront();
_currentPageIndex = value;
}
}
}
public void AddPage(Control page)
{
Controls.Add(page);
page.Dock = DockStyle.Fill;
}
}
And then add pages and set current visible page:
MultiPagePanel p;
// MyTabPage is a Control derived class that represents one page on your form.
MyTabPage page = new MyTabPage();
p.AddPage(page);
p.CurrentPageIndex = 0;
I was needing this code but in VB.net so I converted it. If someone needs this code in VB.Net there it is
Imports System
Imports System.Windows.Forms
Public Class TablessControl
Inherits System.Windows.Forms.TabControl
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' Hide tabs by trapping the TCM_ADJUSTRECT message
If (m.Msg = Convert.ToInt32("0x1328", 16) And Not DesignMode) Then
m.Result = CType(1, IntPtr)
Else
MyBase.WndProc(m)
End If
End Sub
End Class
and thanks to @Hans Passant for the answer in C#
To complement Hans Passant's existing answer, I've found four ways to hide the arrows from the user when the numbers of tabs exceeds the width of the TablessControl. No single solution is necessarily perfect for everyone, but may be for you (or at least a combination of them).
Solution 1:
Simply enable Multiline
. This will prevent the arrows from appearing in the first place. However, bear in mind, you may lose WYSIWYG in the designer because the vertical space will be adjusted downwards vertically, and controls within the TablessControl may even be 'chopped off' at the bottom (again, only in developer mode though).
Solution 2:
A more advanced solution which solves the WYSIWYG problem above is to only enable Multiline
once the program gets running. Simply add this constructor to the TablessControl class:
public TablessControl()
{
bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
if (!designMode) Multiline = true;
}
To the developer, they will still appear as a single line of tabs.
Solution 3:
Decrease the font size of the TablessControl. Each tab should shrink accordingly. Since the user never gets to see the tabs, it shouldn't matter much if you set the font sizes to even 4pt.
However be careful, because the TablessControl's contents may also be resized. If this happens, re-edit the font size for each widget inside, and at that point, they'll thankfully stay at that size even if you then decide to re-change the main TablessControl's font size again.
This approach also has the advantage of more closely showing the true WYSIWYG vertical real-estate to the developer (which can look fine for the user, but may be cut off slightly at the bottom in the designer due to the height of the tabs).
This solution can be combined with Solution 1 and 2 for accumulated advantages.
Solution 4:
This solution isn't necessarily so great if any of the tabs have text which are long. Thanks to Hans for suggesting it.
First set the TablessControl's SizeMode
to 'Fixed', and then reduce the TablessControl's ItemSize
Width
property to a smaller number to reduce each tab's width. Feel free also to adjust the ItemSize
Height
property to help address the aforementioned WYSIWYG issue, though Solution 3 may be more helpful for that problem.
This solution can be combined with the above solutions to further accumulate advantages.
If you really want to do this, yo can do something like this
tcActionControls.Region = new Region(new RectangleF(
tbPageToShow.Left,
tbPageToShow.Top,
tbPageToShow.Width,
tbPageToShow.Height)
);
Where tcActionControls
is your TabControl
and tbPageToShow
is a TabPage
to show in this precise moment.
Should work for you.
Regards.
This solution appears to work well - How to hide tabs in the tab control?
Insert Tabcontrol into a form, the default name being tabcontrol1.
Ensure that tabcontrol1 is selected in the Properties pane in visual studio and change the following properties:
a. Set Appearance to Buttons
b. Set ItemSize 0 for Width and 1 for Height
c. Set Multiline to True
d. Set SizeMode to Fixed
This is best done after your have finished your design time tasks as it hides them in the designer as well - making it difficult to navigate!
You can try removing the TabPage from the TabPageCollection :
TabControl.TabPageCollection tabCol = tabControl1.TabPages;
foreach (TabPage tp in tabCol)
{
if(condition)
{
tabCol.Remove(tp);
}
}
In my WinForms app, I was able to work around this by positioning the TabControl's y-coordinate outside the visible range of the form, so the tabs were effectively hidden. This example only works if the tabControl is near the top of the form, but you get the idea.
private void frmOptions_Load(object sender, EventArgs e)
{
tabControl1.Top = -23; //Only tabPage contents visible
}
精彩评论