locked
Tab Page Font RRS feed

  • Question

  • Hi,

    I have a TabControl on my form. It has 1 TabPage.

    On the Tab Page there are several textboxes that are next to each other vertically (in a column).

    When I change the font size of my TabPage (to make the text bigger on the Tab Page heading), the whole layout of my Tab Page changes and the textboxes are no longer next to each other. They have white space between them now.

    Is there a way I can just change the font size foe the Tab Page without it affecting all the other controls on the Tab Page ?

    J


    jppnn


    • Edited by john pp nn Tuesday, November 24, 2015 10:05 AM
    • Moved by CoolDadTx Tuesday, November 24, 2015 2:58 PM Winforms related
    Tuesday, November 24, 2015 10:05 AM

Answers

  • There is a solution in the below thread it worked for me, you need to set the DrawMode property to OwnerDrawFixed. Also you may need to change the styles used to fit your preference:

    How do I make a TabPage's title text bold?

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
            {
    
                Graphics g = e.Graphics;
                Brush _TextBrush;
    
                // Get the item from the collection.
                TabPage _TabPage = tabControl1.TabPages[e.Index];
    
                // Get the real bounds for the tab rectangle.
                Rectangle _TabBounds = tabControl1.GetTabRect(e.Index);
    
                if (e.State == DrawItemState.Selected)
                {
                    // Draw a different background color, and don't paint a focus rectangle.
                    _TextBrush = new SolidBrush(Color.Blue);
                    g.FillRectangle(Brushes.Gray, e.Bounds);
                }
                else
                {
                    _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
                   // e.DrawBackground();
                }
    
                // Use our own font. Because we CAN.
                Font _TabFont = new Font(e.Font.FontFamily, (float)9, FontStyle.Bold, GraphicsUnit.Pixel);
                //Font fnt = new Font(e.Font.FontFamily, (float)7.5, FontStyle.Bold);
    
                // Draw string. Center the text.
                StringFormat _StringFlags = new StringFormat();
                _StringFlags.Alignment = StringAlignment.Center;
                _StringFlags.LineAlignment = StringAlignment.Center;
                g.DrawString(tabControl1.TabPages[e.Index].Text, _TabFont, _TextBrush,
                             _TabBounds, new StringFormat(_StringFlags));
    
            }


    Fouad Roumieh

    • Marked as answer by john pp nn Tuesday, November 24, 2015 5:10 PM
    Tuesday, November 24, 2015 11:37 AM

All replies

  • There is a solution in the below thread it worked for me, you need to set the DrawMode property to OwnerDrawFixed. Also you may need to change the styles used to fit your preference:

    How do I make a TabPage's title text bold?

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
            {
    
                Graphics g = e.Graphics;
                Brush _TextBrush;
    
                // Get the item from the collection.
                TabPage _TabPage = tabControl1.TabPages[e.Index];
    
                // Get the real bounds for the tab rectangle.
                Rectangle _TabBounds = tabControl1.GetTabRect(e.Index);
    
                if (e.State == DrawItemState.Selected)
                {
                    // Draw a different background color, and don't paint a focus rectangle.
                    _TextBrush = new SolidBrush(Color.Blue);
                    g.FillRectangle(Brushes.Gray, e.Bounds);
                }
                else
                {
                    _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
                   // e.DrawBackground();
                }
    
                // Use our own font. Because we CAN.
                Font _TabFont = new Font(e.Font.FontFamily, (float)9, FontStyle.Bold, GraphicsUnit.Pixel);
                //Font fnt = new Font(e.Font.FontFamily, (float)7.5, FontStyle.Bold);
    
                // Draw string. Center the text.
                StringFormat _StringFlags = new StringFormat();
                _StringFlags.Alignment = StringAlignment.Center;
                _StringFlags.LineAlignment = StringAlignment.Center;
                g.DrawString(tabControl1.TabPages[e.Index].Text, _TabFont, _TextBrush,
                             _TabBounds, new StringFormat(_StringFlags));
    
            }


    Fouad Roumieh

    • Marked as answer by john pp nn Tuesday, November 24, 2015 5:10 PM
    Tuesday, November 24, 2015 11:37 AM
  • Thanks, just what I was looking for.

    J


    jppnn

    Tuesday, November 24, 2015 5:10 PM