Background color for Tabcontrol in C # windows application

I am trying to add a font color and background color for tabs created in a tabcontrol, as well as background colors for empty space without tabs. Because my tabcontrol is stretched to the size of my full screen form. Let's say if only one tab is created in a tabcontrol, the background color of the tab should be blue, and the rest of the tabcontrol should be dark blue. The default color for this is gray, which is very ugly.

I am using the below code which works great for the font color and background color of the tab. But I am not getting bg color for the rest of the places.

Am I missing something?

     private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
    {
            //set color for Background
            Brush BG_BackBrush = new SolidBrush(Color.FromArgb(111, 111, 111)); //Set background color 
            //Rectangle BG_r = e.Bounds;
            Rectangle BG_r = new Rectangle(1682, 34, this.Width - 2, this.Height - 2);
            BG_r = new Rectangle(BG_r.X, BG_r.Y + 8, BG_r.Width, BG_r.Height - 3);
            e.Graphics.FillRectangle(BG_BackBrush, e.Bounds);


            //set color for Tabs
            Font TabFont;
            Brush BackBrush = new SolidBrush(Color.FromArgb(147, 188, 200)); //Set background color
            Brush ForeBrush = new SolidBrush(Color.FromArgb(11, 51, 106));//Set foreground color  


            if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
            {
                TabFont = new Font(e.Font, FontStyle.Bold);
            }
            else
            {
                TabFont = new Font(e.Font, FontStyle.Regular);
            }
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 8, r.Width, r.Height - 3);

            string TabName = TaskBarRef.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;

            e.Graphics.FillRectangle(BackBrush, e.Bounds);
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);

            sf.Dispose();




            if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
    }

      

+2


source to share


1 answer


I would look at this sample code. It shows you how to color the tabs as well as the entire selected control.

In your example, you can change the BackBrush to Solid Solid and add this before the sf.Dispose line:



TaskBarRef.tabControl1.TabPages[e.Index].BackColor = BackBrush.Color;

      

+1


source







All Articles