开发者

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

开发者 https://www.devze.com 2022-12-11 11:38 出处:网络
How do I implement a tab con开发者_JS百科trol with vertical tabs in C#?Create an instance of System.Windows.Forms.TabControl (one of the standard container controls for Windows Forms) and set the Alig

How do I implement a tab con开发者_JS百科trol with vertical tabs in C#?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号