1 tab control, 5 tab pages - different color

G

Guest

I have one tab control with 5 tab pages.
I want to color each tab page a different color.
Does anyone know how to color the entire tab page a unique color?
(that includes the tab part of the tab page)
In other words, just like different paper folders come in different colors,
how do you program this?

If you look at the code I got from Microsoft, this will color only one tab
and put text on only one tab:
HOw does one do it when you have 5 tab pages with one tab control?

tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
....
....

tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
....
....
private void DrawOnTab(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Blue);
Font font = new Font("Arial", 10.0f);
SolidBrush brush = new SolidBrush(Color.Red);
SolidBrush brush1 = new
SolidBrush(Color.Beige);
g.DrawRectangle(p, tabArea);
g.DrawString("tabPage1",font,brush,tabTextArea);
}
 
R

realfun

got an ugly one, hope it's helpful:
------------------------------------------------------
private void DrawOnTab(object sender, DrawItemEventArgs e)
{
if (e.State == DrawItemState.Selected)
{
TabControl tc = (TabControl) sender;
TabPage currentTab = tc.TabPages[tc.SelectedIndex];
Graphics g = e.Graphics;
Font font = new Font("Arial", 10.0f);
SolidBrush brushRed = new SolidBrush(Color.Red);
Color[] backColors = new Color[3]{Color.Blue,
Color.Green, Color.Black};
Rectangle tabArea = e.Bounds;
Rectangle tabTextArea = e.Bounds;
Brush backBrush = new
SolidBrush(backColors[tc.SelectedIndex]);
g.FillRectangle(backBrush, tabArea);
currentTab.BackColor = backColors[tc.SelectedIndex];
g.DrawString(currentTab.Text, font, brushRed,
tabTextArea);
}
}

private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
tabControl1.Invalidate();
}
 
G

Guest

realfun almost has a perfect solution, however the non active tabs turned to
grey color instead of keeping their color. In other words, the only tab that
has the right color is the active tab (one just clicked) Any ideas on how to
fix it?
Martha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top