akb
akb

Reputation: 43

C# Tabs Rotate Text

I am having some issues rotating the text on a tab. The tabs originally worked just fine, but then I wanted to bold the text when selected, so I used the Draw Item Event. I added a RotateTransform and a TranslateTransform, but its not working. The text just doesn't show up. I have troubleshot it and if I take the rotation away, the text is visible, but when I use the rotate to make the text vertical, it disappears. Here's my code:

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        string tabText = tabControl1.TabPages[e.Index].Text;
        SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
        Brush _textBrush = Brushes.Black;
        TabPage _tabPage = tabControl1.TabPages[e.Index];
        System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
        StringFormat _stringFlags = new StringFormat();
        _stringFlags.Alignment = StringAlignment.Center;
        _stringFlags.LineAlignment = StringAlignment.Center; 

        PointF tabPt = new PointF(_tabBounds.Left+(_tabBounds.Width), _tabBounds.Top+(_tabBounds.Height));

        if (e.Index == tabControl1.SelectedIndex)
        {
            g.RotateTransform(-90);
            g.TranslateTransform(tabPt.X, tabPt.Y);

            g.DrawString(tabControl1.TabPages[e.Index].Text,
                new Font(tabControl1.Font, FontStyle.Bold),
                _textBrush,
                new PointF(tabPt.X, tabPt.Y));
            g.ResetTransform();
        }
        else
        {
            g.TranslateTransform(tabPt.X, tabPt.Y);                
           g.RotateTransform(-90);

            g.DrawString(tabControl1.TabPages[e.Index].Text,
                tabControl1.Font,
                _textBrush,
                new PointF(tabPt.X,tabPt.Y));
            g.ResetTransform();
        }
    }
}

Any help would be greatly appreciated.

EDIT Here's the image:

enter image description here

Here's the new code:

        Graphics g = e.Graphics;
        string tabText = tabControl1.TabPages[e.Index].Text;
        SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
        Brush _textBrush = Brushes.Black;
        TabPage _tabPage = tabControl1.TabPages[e.Index];
        System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
        PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));
        PointF tabPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (2.75F), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width)/2);
        if (e.Index == tabControl1.SelectedIndex)
        {
            g.TranslateTransform(rotPt.X, rotPt.Y);
            g.RotateTransform(-90);
            g.TranslateTransform(-(rotPt.X), -(rotPt.Y));

            g.DrawString(tabText,
                new Font(tabControl1.Font, FontStyle.Bold),
                _textBrush,
                new PointF(rotPt.X, rotPt.Y));
        }
        else
        {
            g.TranslateTransform(rotPt.X, rotPt.Y);
            g.RotateTransform(-90);
            g.TranslateTransform(-rotPt.X, -rotPt.Y);

            g.DrawString(tabText,
                tabControl1.Font,
                _textBrush,
                new PointF(rotPt.X,rotPt.Y));
        }

Upvotes: 1

Views: 447

Answers (1)

akb
akb

Reputation: 43

Thanks TaW for your help. Here's the final code and its working.

   public form1()
    {
        InitializeComponent();            
        tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
        tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)

    {
        Graphics g = e.Graphics;
        string tabText = tabControl1.TabPages[e.Index].Text;
        SizeF textSize = g.MeasureString(tabText, tabControl1.Font);
        Brush _textBrush = Brushes.Black;
        TabPage _tabPage = tabControl1.TabPages[e.Index];
        System.Drawing.Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
        PointF rotPt = new PointF(_tabBounds.Left + (_tabBounds.Width / 2) - (textSize.Height / 2), _tabBounds.Top + (_tabBounds.Height / 2) + (textSize.Width/2));

        if (e.State.HasFlag(DrawItemState.Selected))
        {
            g.TranslateTransform(rotPt.X, rotPt.Y);
            g.RotateTransform(-90);
            g.TranslateTransform(-(rotPt.X), -(rotPt.Y));

            g.DrawString(tabText,
                new Font(tabControl1.Font, FontStyle.Bold),
                _textBrush,
                new PointF(rotPt.X, rotPt.Y));
            g.ResetTransform();
        }

        else
        {
            g.TranslateTransform(rotPt.X, rotPt.Y);
            g.RotateTransform(-90);
            g.TranslateTransform(-rotPt.X, -rotPt.Y);

            g.DrawString(tabText,
                tabControl1.Font,
                _textBrush,
                new PointF(rotPt.X,rotPt.Y));
            g.ResetTransform();
        }
    }

P.S. I never did get the e.Bounds rather than GetTabRect (i'm not sure how to set it to the selected tab).

Upvotes: 1

Related Questions