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

Share:
13,016

Related videos on Youtube

Itay.B
Author by

Itay.B

Updated on June 04, 2022

Comments

  • Itay.B
    Itay.B almost 2 years

    How do I implement a tab control with vertical tabs in C#?

    • Bob Kaufman
      Bob Kaufman over 14 years
      Ah... that's better! When I thought of "vertical tab", I was thinking of the action (i.e., VTAB) not the control.
    • user6170001
      user6170001 over 14 years
      Bob: yeah... I was all set to post "easy, just use \v"... grin
  • Michael Todd
    Michael Todd about 12 years
    And, if you'd prefer the text to be displayed horizontally as well, perform the steps here.