-
Jul 7th, 2009, 12:12 PM
#1
Thread Starter
Junior Member
Changing TabControl Backcolor
Hi,
I tried to change the TabControl back color but only succeeded changing the tabs back color (with the code below) but not the tab control color (which is behind the tabs, in the upper right corner).
HELP!
many thanks
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
'Firstly we'll define some parameters.
Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
Dim FillBrush As New SolidBrush(Color.LightGray)
Dim TextBrush As New SolidBrush(Color.Black)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
'If we are currently painting the Selected TabItem we'll
'change the brush colors and inflate the rectangle.
If CBool(e.State And DrawItemState.Selected) Then
'' FillBrush.Color = Color.LightGray
'' TextBrush.Color = Color.Black
ItemRect.Inflate(2, 2)
End If
'Set up rotation for left and right aligned tabs
If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
Dim RotateAngle As Single = 90
If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
e.Graphics.TranslateTransform(cp.X, cp.Y)
e.Graphics.RotateTransform(RotateAngle)
ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
End If
'Next we'll paint the TabItem with our Fill Brush
e.Graphics.FillRectangle(FillBrush, ItemRect)
'Now draw the text.
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)
'Reset any Graphics rotation
e.Graphics.ResetTransform()
'Finally, we should Dispose of our brushes.
FillBrush.Dispose()
TextBrush.Dispose()
End Sub
-
Jul 7th, 2009, 12:16 PM
#2
Re: Changing TabControl Backcolor
Are you talking about the unoccupied space to the right of the actual tabs in the tabcontrol? This will take on whatever color the container is. So for example if your forms background color was red, and you place a tabcontrol on it, the tabcontrols area in the upper right will be red as well.
If it is a case where you want this color to actually be different from the form, then add a panel to your form, and set the panel to whatever color you want. Then move your tabcontrol into the panel, and set its dock property to fill so it takes up the full space of the panel control.
The TabControl doesn't expose a BackColor property (it inherits it from the base class Control, but it hides it, and has no use)
In fact, this is totally valid code
Code:
TabControl1.BackColor = Color.red
it will compile and everything, but internally this property is simply ignored by the tabcontrol.
-
Jul 7th, 2009, 05:29 PM
#3
Re: Changing TabControl Backcolor
Check out this site, there's loads of controls and tips on custom tabcontrol drawing.
-
Jul 8th, 2009, 03:27 PM
#4
Thread Starter
Junior Member
Re: Changing TabControl Backcolor
Yes, i'm talking about unoccupied space to the right of the actual tabs in the tabcontrol. Unfortunattly, none of the advices you gave were helpful
Any ideas?
Thank in advance
-
Jul 8th, 2009, 03:32 PM
#5
Re: Changing TabControl Backcolor
Why were non of the advices helpful?
I think kleinma's answer tells you exactly what you need to do: put your tabcontrol in a container (Panel for example) and set the BackColor of the Panel. DONE!
-
Jul 8th, 2009, 03:33 PM
#6
Hyperactive Member
Re: Changing TabControl Backcolor
Not sure I totally know whats going on here but can you try putting the tabcontrol on a panel and then setting its backcolor to the backcolor you want?
If I helped you please rate me.
-
Jul 8th, 2009, 03:35 PM
#7
Re: Changing TabControl Backcolor
Originally Posted by ngreenwood6
Not sure I totally know whats going on here but can you try putting the tabcontrol on a panel and then setting its backcolor to the backcolor you want?
See the first reply above
-
Jul 8th, 2009, 03:39 PM
#8
Hyperactive Member
Re: Changing TabControl Backcolor
I read the first part of your reply but skipped over the last half lol. Do what he says lol :;
If I helped you please rate me.
-
Jul 8th, 2009, 04:03 PM
#9
Thread Starter
Junior Member
Re: Changing TabControl Backcolor
I understood what he was saying it simply doesn't work. I opened a new project with a tab control only trying to isolate the problem.
Now the space is filled with the form back color, however i cannot change the tabs headers back color. when i do that with the TabControl1_DrawItem function, the headers color is changed to what i want, but the space is changed to some kind of gray and i don't have control over it - back to square one. ((
i guess my problem in one sentence is : how do i control the entire TabControl back color - tabs, tabs headers and that space in the corner?
-
Jul 8th, 2009, 07:13 PM
#10
Re: Changing TabControl Backcolor
ok.. go REread what he instructed to do.
do NOT drop the TabControl on the form itself. Put a Panel control on the form. THEN, add the TabControl to the panel. Set the PANEL's BackColor property to whatever color you are trying to achieve as the BackColor of the tabcontrol.
Last edited by stateofidleness; Jul 8th, 2009 at 07:17 PM.
-
Jul 9th, 2009, 01:59 AM
#11
Thread Starter
Junior Member
Re: Changing TabControl Backcolor
I already did that and as you can see in the picture, the tabs headers backcolor is not changed, only the space in the upper right corner. I need to change both.
I managed to overcome the problem by adding the following code:
Dim r As Rectangle = TabControl1.GetTabRect(TabControl1.TabPages.Count - 1)
Dim rf As RectangleF = New RectangleF(r.X + r.Width, r.Y - 5, TabControl1.Width - (r.X + r.Width), r.Height + 5)
Dim b As Brush = New SolidBrush(Color.Yellow)
e.Graphics.FillRectangle(b, rf)
b.Dispose()
I got the code from the following links:
http://blog.villainousmind.com/2009/...ol-in-net.html
which led me to
http://stackoverflow.com/questions/6...ontrols-in-net
Last edited by kiss987; Jul 9th, 2009 at 02:24 AM.
-
Jul 9th, 2009, 08:40 AM
#12
Re: Changing TabControl Backcolor
good deal. thanks for sharing the code.
-
Jul 9th, 2009, 11:26 AM
#13
Re: Changing TabControl Backcolor
Originally Posted by kiss987
Yes, i'm talking about unoccupied space to the right of the actual tabs in the tabcontrol.
Right...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|