How do I implement a tab control with vertical tabs in C#?
13,016
Solution 1
Create an instance of System.Windows.Forms.TabControl (one of the standard container controls for Windows Forms) and set the Alignment property to Left.
Solution 2
First set in properties the Alignment property to Left.
Second set SizeMode property to Fixe.
Third set ItemSize property to prefered size example width :30 height :120.
After that you need to set the DrawMode property to OwnerDrawFixed. Next step is define a handler for the DrawItem event of TabControl that renders the text from left to right.
Example In form Designers.cs file
TabControl.DrawItem += new DrawItemEventHandler(tabControl_DrawItem);
Definition for tabControl_DrawItem method:
private void tabControl_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush _textBrush;
// Get the item from the collection.
TabPage _tabPage = TabControl.TabPages[e.Index];
// Get the real bounds for the tab rectangle.
Rectangle _tabBounds = TabControl.GetTabRect(e.Index);
_textBrush = new System.Drawing.SolidBrush(Color.Black);
// Use our own font.
Font _tabFont = new Font("Arial", (float)12.0, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
}
Effect:Ready horizontal tabcontrol
I was based on https://msdn.microsoft.com/en-us/library/ms404305(v=vs.110).aspx
Related videos on Youtube
Author by
Itay.B
Updated on June 04, 2022Comments
-
Itay.B almost 2 years
How do I implement a tab control with vertical tabs in C#?
-
Bob Kaufman over 14 yearsAh... that's better! When I thought of "vertical tab", I was thinking of the action (i.e., VTAB) not the control.
-
user6170001 over 14 yearsBob: yeah... I was all set to post "easy, just use \v"... grin
-
-
Michael Todd about 12 yearsAnd, if you'd prefer the text to be displayed horizontally as well, perform the steps here.