In VB6, there was an issue of not being able to color just the tabs of the tab control. It appears this is still the case in .net, yes? Does anyone have a way to paint the tabs of the tab control, I'm stuck :(
thanks
kevin
Printable View
In VB6, there was an issue of not being able to color just the tabs of the tab control. It appears this is still the case in .net, yes? Does anyone have a way to paint the tabs of the tab control, I'm stuck :(
thanks
kevin
ok, I guess it is still a problem, and I will assume no one has it figured out yet without but a third party control..... so.......
how could I figure out where the tabs are and use a graphic method to color them manually? or
how can I get the area in the image below?
Any idea's?
kevin
Have you tried Inheriting the Tab ccontrol in a class and overridding the OnPaint event?
that's how I would do it, but the problem I see is getting the right edge of the last tab since I only want to paint the area to the right of all of the tabs so it doesn't look all corny and stuff :(
kevin
Ah, not the actual tabs but the blank area. How about drawing over it using the Graphics namespace? Since it probably doesnt
move or resize it should be easy to cover it with a color or gradient you want.
yeah, I'm gonna work on it....in the mean time I found this code to paint the tabs here and it works pretty well :thumb:
just drop a tab control on a form with DrawMode = OwnerDrawFixed and put a couple of tab pages in it
VB Code:
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.Red) Dim TextBrush As New SolidBrush(Color.White) 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.White TextBrush.Color = Color.Red 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
Very nice :thumb: Thanks for sharing it. I dont think I remember seeing any code on the Forums for .NET Tab control tab coloring.
I have worked on it, but don't see why it doesn't work. using mouse move events, it appears the the blank region is part of the form (at least it responds to form events so I have tried this codeQuote:
Originally Posted by RobDog888
based on the mousemove feedback, the rectanlge looks like it's in the right spot, but it doesn't paint over the blank area. Can you see what's wrong? Maybe I'm painting on the wrong object?VB Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim fb As New SolidBrush(Color.Black) 'Me.BackColor) Dim LastTabRect As Rectangle = TabControl1.GetTabRect(TabControl1.TabPages.Count - 1) Dim Rect As Rectangle Rect.Location = New Point(LastTabRect.Right + TabControl1.Left, TabControl1.Top) Rect.Size = New Size(TabControl1.Right - Rect.Left, LastTabRect.Height) e.Graphics.FillRectangle(fb, Rect) fb.Dispose() Label2.Text = "rect location = " & Rect.X & ", " & Rect.Y & ", Size = " & Rect.Width & ", " & Rect.Height End Sub
here's the project too.
thanks
kevin
Your painting behind the tab control, on the form and not on the tab control.
You need to Inherit in a classs the tab control so you can get access to the OnPaint event. There may be another
way to draw on the control without inheriting. I'll look into it.
This is another option for drawing on the tab control without inheriting. Although I didnt get it to paint yet,
but the option is there.
VB Code:
TabControl1.CreateGraphics.FillRectangle(fb, Rect)
got it!.....I just had my rect coords off I guess :o
thanks
for your help
kevin
I just go it too. The issue I seen was that the code needed to be in the DrawItem procedure and not
the Form_Paint procedure.
I didnt realize either that you were using OwnerDrawnFixed also. ;)
Hi........
thanks a lot........ur coding really helped me to solve my problem in tabcoltrol.....
vidyaa..
hai,wat is the way without inherit to paint tab control? im stuck
Have a look at this site:
http://dotnetrix.co.uk/tabcontrol.htm
It hosts a large number of custom tabcontrols (source code for VB.NET and C#.NET) which do a variety of things including custom drawing it completely, which is probably what you want.
(It's pretty complicated though, but it's all there in the source code)