Results 1 to 3 of 3

Thread: HowTo: Select Tab Caption Bold?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    HowTo: Select Tab Caption Bold?

    I am tryin to set the text of the selected tab to bold but theres no property to do this so I tried to override the drawing of the tabs.

    VB Code:
    1. private void tab_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    2. {
    3.     tabArea = tab.GetTabRect(e.Index);
    4.     tabTextArea = (RectangleF)tab.GetTabRect(e.Index);
    5.     StringFormat drawFormat = new StringFormat();
    6.     drawFormat.Alignment = StringAlignment.Center;
    7.     SolidBrush b = new SolidBrush(Color.Black);
    8.     Graphics g = e.Graphics;
    9.     Pen p = new Pen(Color.White);
    10.     System.Drawing.Font font;
    11.     if (e.Index == tab.SelectedIndex)
    12.     {
    13.         font = new Font(tab.Font.FontFamily,tab.Font.Size , FontStyle.Bold, GraphicsUnit.Point);
    14.     }
    15.     else
    16.     {
    17.         font = tab.Font;
    18.         g.DrawRectangle(p, tabArea);
    19.     }
    20.     g.DrawString(tab.TabPages[e.Index].Text, font, b, tabTextArea, drawFormat);
    21. }

    I have a few problems with this. One is it puts the text at the top of the tab instead of the middle I'm not sure how to fix that. Second it's writting the text as multiline. Third i draw the line but with pen but don't know how to keep it from appearing under the selected tab. But the bold works Does anyone kno how i can fix these problems or another way of doing this?
    Last edited by Tewl; Dec 7th, 2003 at 01:13 AM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Look up how to use MeasureString .

    Code:
    Graphics gfx;
    gfx.MeasureString

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    Thanks pirate I'm having some trouble with that method tho. I have been reading on msdn and it seemed that I needed to measure the string then adjust the rectanges width and height but I am having problems with this.

    VB Code:
    1. private void tab_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    2. {
    3.     tabArea = tab.GetTabRect(e.Index);
    4.     StringFormat drawFormat = new StringFormat();
    5.     drawFormat.Alignment = StringAlignment.Center;
    6.     drawFormat.LineAlignment = StringAlignment.Center;
    7.     SolidBrush b = new SolidBrush(Color.Black);
    8.     Pen p = new Pen(Color.White);
    9.     Font font = tab.Font;
    10.     if (e.Index == tab.SelectedIndex)
    11.     {
    12.         p = new Pen(this.BackColor);
    13.         font = new Font(tab.Font.FontFamily,tab.Font.Size , FontStyle.Bold, GraphicsUnit.Point);
    14.         /*
    15.            SizeF stringSize = new SizeF();
    16.            stringSize = e.Graphics.MeasureString(tab.TabPages[e.Index].Text, font);
    17.            tabArea.Width = (int)stringSize.Width;
    18.            tabArea.Height = (int)stringSize.Height;
    19.         */
    20.     }
    21.     e.Graphics.DrawRectangle(p, tabArea);
    22.     e.Graphics.DrawString(tab.TabPages[e.Index].Text, font, b, (RectangleF)tabArea, drawFormat);
    23. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width