Tab Control with Icons


D King

Hi. I have a Tab control on my form. I used an image list for icons for my
tabs. Then I decide I wanted to make the tabs look a little better so I set
the DrawMode to OwnerDrawFixed, wrote some code to the TabControls DrawItem
event and now I have the selected tab with a nice gradient blue backcolor.
Problem is, I dont' know how to write the code to draw my icons. Does anyone
know or have any examples? Thanks in advance....

Private Sub TabControlMain_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles TabControlMain.DrawItem

Dim myFont As Font
Dim backBrush, foreBrush As Brush

If e.Index = Me.TabControlMain.SelectedIndex Then
myFont = New Font(e.Font, FontStyle.Bold)

'== make the selected tab have a gradient blue background
backBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds,
Color.Blue, Color.DeepSkyBlue,
foreBrush = Brushes.PowderBlue
myFont = e.Font
'== dim the font of the non selected tabs
backBrush = New SolidBrush(SystemColors.Control)
foreBrush = New SolidBrush(Color.DimGray)

End If

Dim tabName As String = Me.TabControlMain.TabPages(e.Index).Text
Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center
e.Graphics.FillRectangle(backBrush, e.Bounds)
Dim r As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y + 4,
e.Bounds.Width, e.Bounds.Height - 4)
e.Graphics.DrawString(tabName, myFont, foreBrush, r, sf)


If e.Index = Me.TabControlMain.SelectedIndex Then
End If

End Sub

Yasutaka Ito

Seems like you're into control development... in a sort.
I suggest you visit, if you haven't. It's one place bunch of
control developers flock and generously share codes, for FREE. You might
find some nice samples that does just what you're looking for.

Sorry, I'm not there yet to be able to give advice on actual code...

good luck!

D King

Geez, I must of been sleeping that day..

Basically this will do the trick on those owner drawn tabs....
Dim iCon As Icon = New Icon("Defects.ico")
e.Graphics.DrawIcon(iCon, e.Bounds.Left + 2, e.Bounds.Top + 2)

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