Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let me know how to change the Tab control's background color as per my application need. Also background color of tabs for each page should be changed.

Regards,

Sushil Saini
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jan-11 22:52pm    
"Page" means ASP.NET, right?
Venkatesh Mookkan 10-Jan-11 22:57pm    
Are you talking about Visual Studio tabs or your application tab? Is your application Web or Windows?
sk saini 21-Jan-11 5:07am    
My Application is windows based and I am talking about my application tab.

Inorder to change background color of tabs,set the TabControl's DrawMode to OwnerDraw, and then handle the DrawItem event to draw.
C#
//set the drawmode and subscribe to the DrawItem event
this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;



C#
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            Font f;
            Brush backBrush;
            Brush foreBrush;

            if(e.Index == this.tabControl1.SelectedIndex)
            {
                f = new Font(e.Font, FontStyle.Italic | FontStyle.Bold);
                backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.Blue, Color.Red, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
                foreBrush = Brushes.PowderBlue;
            }
            else
            {
                f = e.Font;
                backBrush = new SolidBrush(e.BackColor);
                foreBrush = new SolidBrush(e.ForeColor);
            }

            string tabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(backBrush, e.Bounds);
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
            e.Graphics.DrawString(tabName, f, foreBrush, r, sf);

            sf.Dispose();
            if(e.Index == this.tabControl1.SelectedIndex)
            {
                f.Dispose();
                backBrush.Dispose();
            }
            else
            {
                backBrush.Dispose();
                foreBrush.Dispose();
            }
        }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 0:21am    
Anupama, are you saying just assignment to BackColor property is not enough? Why?
Anupama Roy 11-Jan-11 0:30am    
BackColor is for the color of Tab Page to change.Solution I provided is for changing the color of the tab for each page.
sk saini 21-Jan-11 5:26am    
Thanks, its really useful for me and works properly.
Sorry for late reply. As I've open my post today and got your reply that is why replaying late.

Thanks again.
Sushil Saini
Anupama Roy 21-Jan-11 5:28am    
You are welcome.Please accept the answer as it helped you.Thanks!
sk saini 22-Jan-11 4:58am    
Isn't there any way not to set TabControl's DrawMode property to OwnerDraw because after doing this I have to write my all tab designing code manually. Earlier I used drag drop control from toolbox for this purpose and have also written codes on it.
This[^] post might help.
 
Share this answer
 
change the back color of the individual tab pages in the form load event...
TabControl1.TabPages(0).BackColor = Color.White


Hope this will give an idea.
 
Share this answer
 
Comments
Member 11774773 13-Oct-16 16:34pm    
This is property not method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900