srikanth2321 -2 Junior Poster in Training

Hi,

I found many examples in the Google to change the color of the tab but I was not able to find a proper solution to change the color of the dead space which is right next to the tab buttons in the tabcontrol. The code which I used is

private void tabControl1_DrawItem(System.Object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            System.Drawing.Graphics g = e.Graphics;
            TabPage tp = tabControl1.TabPages[e.Index];
            System.Drawing.Brush br = null;
            System.Drawing.Brush colBorder = null;
            System.Drawing.StringFormat sf = new System.Drawing.StringFormat();
            System.Drawing.RectangleF r ;

            System.Drawing.RectangleF rBorder = new System.Drawing.RectangleF(tabControl1.Left, tabControl1.Top, tabControl1.Width, tabControl1.Height);

            sf.Alignment = System.Drawing.StringAlignment.Center;

            string strTitle = tp.Text;
            
            if ((e.State == DrawItemState.None))
            {
                System.Drawing.RectangleF rBack = new System.Drawing.RectangleF(tabControl1.Left, tabControl1.Top, tabControl1.Width, tp.Bounds.Top - 2);
                //

                //this is the background color of the tabpage
                //you could make this a stndard color for the selected page
                br = new System.Drawing.SolidBrush(tp.BackColor);

                //this is the background color of the tab page
                g.FillRectangle(br, rBack);

                br.Dispose();
            }

            //If the current index is the Selected Index, change the color
            if (tabControl1.SelectedIndex == e.Index)
            {
                r = new System.Drawing.RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2);

                //this is the background color of the tabpage
                //you could make this a stndard color for the selected page
                br = new System.Drawing.SolidBrush(tp.BackColor);

                colBorder = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);

                //this is the background color of the tab
                g.FillRectangle(br, e.Bounds);

                //this is the background color of the tab page
                //you could make this a stndard color for the selected page
                br.Dispose();

                br = new System.Drawing.SolidBrush(tp.ForeColor);

                r.Offset(0, 1);

                g.DrawString(strTitle, tabControl1.Font, br, r, sf);

                br.Dispose();

                colBorder.Dispose();


            }
            else
            {
                //these are the standard colors for the unselected tab pages
                br = new System.Drawing.SolidBrush(tp.BackColor);

                r = new System.Drawing.RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height + 2);

                System.Drawing.RectangleF rBack = new System.Drawing.RectangleF(tabControl1.Left + 1, tabControl1.Top + e.Bounds.Y + e.Bounds.Height + 3, tabControl1.Width - 3, tabControl1.Height - (e.Bounds.Y + e.Bounds.Height + 4));

                //this is the background color of the tab page
                g.FillRectangle(br, rBack);

                //this is the background color of the tab button
                g.FillRectangle(br, r);

                br.Dispose();
                br = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

                r.Offset(0, 3);

                g.DrawString(strTitle, tabControl1.Font, br, r, sf);

                br.Dispose();

            }

            //Dispose objects
            sf.Dispose();
        }

Any help would be great to solve my everlasting problem.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.