Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Go Back  Xtreme Visual Basic Talk > > > Setting TabControl header back color of specific tabs?


Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2013, 10:24 AM
mrkamran's Avatar
mrkamran mrkamran is offline
Centurion
 
Join Date: Jun 2003
Posts: 188
Question Setting TabControl header back color of specific tabs?


Hi,

I want to set specific backcolor of specific tab, I have following code which can change backcolor of selected tab but I want to set color through loop of tab index. Suppose I have 8 tabs I want to set red color for 2, 5 and 6 tab index only on new form load event. please help in this regard.

Private Sub TabEmployeeDetail_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabEmployeeDetail.DrawItem
Dim g As Graphics = e.Graphics
Dim tp As TabPage = TabEmployeeDetail.TabPages(e.Index)
Dim br As Brush
Dim sf As New StringFormat
Dim idx As Integer = e.Index

Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

sf.Alignment = StringAlignment.Center

Dim strTitle As String = tp.Text

'If the current index is the Selected Index, change the color
If TabEmployeeDetail.SelectedIndex = e.Index Then
'this is the background color of the tabpage header
br = New SolidBrush(Color.BurlyWood) ' chnge to your choice
g.FillRectangle(br, e.Bounds)
'this is the foreground color of the text in the tab header
br = New SolidBrush(Color.Black) ' change to your choice
g.DrawString(strTitle, TabEmployeeDetail.Font, br, r, sf)

Else
'these are the colors for the unselected tab pages
br = New SolidBrush(Color.WhiteSmoke) ' Change this to your preference
g.FillRectangle(br, e.Bounds)
br = New SolidBrush(Color.Black)
g.DrawString(strTitle, TabEmployeeDetail.Font, br, r, sf)
End If
End If

End Sub

Is there any way to loop through all tabs and set backcolor of specific tabs?

Thanks

Kamran
__________________
Thoughts determine what you want...Action determines what you get!
Reply With Quote
  #2  
Old 04-16-2013, 11:13 AM
Gruff's Avatar
GruffSetting TabControl header back color of specific tabs? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Of course there is.
The DrawItem() Event fires for every tab on your tabcontrol.

If you read your code you will see it is filtering on the SelectedIndex. (The selected tab)

You just have to filter which tabs you want to redraw.
Code:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed End Sub Private Sub TabControl1_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem Dim g As Graphics = e.Graphics Dim tp As TabPage = TabControl1.TabPages(e.Index) Dim br As Brush = Nothing Dim fn As Font = New Font(TabControl1.Font.Name, TabControl1.Font.Size, FontStyle.Bold) Dim sf As New StringFormat Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2) Dim strTitle As String = tp.Text sf.Alignment = StringAlignment.Center Select Case e.Index '<--- e.index is zero based. Case 1, 4, 5 '<--- Draw only if tab is 2, 5 or 6. br = New SolidBrush(Color.Red) g.FillRectangle(br, e.Bounds) br = New SolidBrush(Color.White) g.DrawString(strTitle, fn, br, r, sf) End Select End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 04-17-2013, 05:09 AM
mrkamran's Avatar
mrkamran mrkamran is offline
Centurion
 
Join Date: Jun 2003
Posts: 188
Default

Thanks Gruff, It works but how can I clear all tabs backcolor at once. for example when I click a button it will set specific tabs backcolors and when I press other button, tab should be reset and no backcolor should be show. My 2nd question is, is there any simple way to change only forecolor of specific tabs through loop from 0 to last tab index? I don't want to use TabControl_DrawItem event.
__________________
Thoughts determine what you want...Action determines what you get!
Reply With Quote
  #4  
Old 04-17-2013, 08:56 AM
Gruff's Avatar
GruffSetting TabControl header back color of specific tabs? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

DrawItem is the only way to draw on the tab headers.
You cannot draw just the forecolor text.

This is because the tab control has to automatically redraw the headers when
ever it thinks it needs to. The DrawItem() event is timed to take place after each tab does its automatic redraw. If you tried to draw on the tab without
using redrawitem the control would wipe out whatever you drew.

Using the default background color you clear the header with a filled rectangle and then draw the text. All this is exactly like in the example.

From your point of view all the tabs will redraw at the same time.

If you use the TabControl invalidate method it should force the tabs to redraw.

As far as toggling and clearing you will probably have to use a form level variable
in your drawitem event to determine which kind of redraw you want to happen.

On your button Click...

Code:
Dim UseDefaultColors as boolean = True Private Button1_Click(sender as object, e as ...) handles Button1.Click UseDefaultColor = False TabControl1.Invalidate <-- Cause Control to redraw End Sub
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 04-17-2013 at 09:10 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs? Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
 
Setting TabControl header back color of specific tabs?
Setting TabControl header back color of specific tabs?
 
-->