Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tabs

winforms

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

like image 808
Itay.B Avatar asked Nov 12 '09 19:11

Itay.B


2 Answers

Create an instance of System.Windows.Forms.TabControl (one of the standard container controls for Windows Forms) and set the Alignment property to Left.

like image 167
BlueMonkMN Avatar answered Sep 30 '22 22:09

BlueMonkMN


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

like image 38
ProgShiled Avatar answered Oct 01 '22 00:10

ProgShiled