Windows Develop Bookmark and Share   
 index > Windows Forms General > tabcontrol question
 

tabcontrol question

At runtime, I want to change the color of the top of the tab to red. I have included a link to a picture of the area of the tab I am talking about. Please let me know how to do this.

Thanks

http://img20.imageshack.us/my.php?image=tabag8.jpg
cheatcountry  Monday, December 17, 2007 2:36 AM

Hi,

Well tabitem has an event called DrawItem. You will need to hook to that event and draw it out.

Check a sample over here :

C# :

Code Block

private void tabPage_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

{

Font f = null;

Brush backBrush = null;

Brush foreBrush = null;

if (e.Index == this.tabPage.SelectedIndex)

{

f = new Font(e.Font, FontStyle.Regular);

backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(205, 225, 255), Color.FromArgb(175, 200, 245), System.Drawing.Drawing2D.LinearGradientMode.Vertical);

foreBrush = Brushes.Black;

}

else

{

f = e.Font;

backBrush = new SolidBrush(e.BackColor);

foreBrush = new SolidBrush(e.ForeColor);

}

string tabName = this.tabPage.TabPages[e.Index].Text;

StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Center;

e.Graphics.FillRectangle(backBrush, e.Bounds);

RectangleF r = new RectangleF((float)(e.Bounds.X), (float)(e.Bounds.Y + 4), (float)(e.Bounds.Width), (float)(e.Bounds.Height - 4));

e.Graphics.DrawString(tabName, f, foreBrush, r, sf);

sf.Dispose();

if (e.Index == this.tabPage.SelectedIndex)

{

f.Dispose();

backBrush.Dispose();

}

else

{

backBrush.Dispose();

foreBrush.Dispose();

}

}

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Monday, December 17, 2007 3:57 AM

Hi,

Well tabitem has an event called DrawItem. You will need to hook to that event and draw it out.

Check a sample over here :

C# :

Code Block

private void tabPage_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

{

Font f = null;

Brush backBrush = null;

Brush foreBrush = null;

if (e.Index == this.tabPage.SelectedIndex)

{

f = new Font(e.Font, FontStyle.Regular);

backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(205, 225, 255), Color.FromArgb(175, 200, 245), System.Drawing.Drawing2D.LinearGradientMode.Vertical);

foreBrush = Brushes.Black;

}

else

{

f = e.Font;

backBrush = new SolidBrush(e.BackColor);

foreBrush = new SolidBrush(e.ForeColor);

}

string tabName = this.tabPage.TabPages[e.Index].Text;

StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Center;

e.Graphics.FillRectangle(backBrush, e.Bounds);

RectangleF r = new RectangleF((float)(e.Bounds.X), (float)(e.Bounds.Y + 4), (float)(e.Bounds.Width), (float)(e.Bounds.Height - 4));

e.Graphics.DrawString(tabName, f, foreBrush, r, sf);

sf.Dispose();

if (e.Index == this.tabPage.SelectedIndex)

{

f.Dispose();

backBrush.Dispose();

}

else

{

backBrush.Dispose();

foreBrush.Dispose();

}

}

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Monday, December 17, 2007 3:57 AM

You can use google to search for other answers

Custom Search

More Threads

• Items in DataGridViewComboBoxColumn from enum giving strange error
• jpeg support of 12 and 16 bit images
• How do you disable focus of a control....
• listbox visible items
• DataGridView OnCellStyleContentChange()
• problem with WebBrowser control
• DOWN LOAD WORD DOCUMENTS FROM THE INTERNET
• How to add AutoUpdate feature in VB.net Apllication
• How to dynamically load the WinForm during run-time?
• Intercepting Form Show method