hello
am trying to build a custom tab control in visual basic.net
i have a problem in fixing a simple problem. the code runs ok
but when i select a tab i no longer see the text that was suppose to be on the tab
and when i select an other tab i no see it but i can't see the text on the tab i selected.
please give me a help
here is my code
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class TC1
Inherits Windows.Forms.TabControl
Public Sub New()
SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint Or Windows.Forms.ControlStyles.ResizeRedraw Or Windows.Forms.ControlStyles.UserPaint Or Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
DoubleBuffered = True
SizeMode = Windows.Forms.TabSizeMode.Fixed
ItemSize = New Size(44, 136)
End Sub
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
Alignment = Windows.Forms.TabAlignment.Left
End Sub
Function ToPen(ByVal color As Color) As Pen
Return New Pen(color)
End Function
Function ToBrush(ByVal color As Color) As Brush
Return New SolidBrush(color)
End Function
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim DB As New Bitmap(Width, Height)
Dim DG As Graphics = Graphics.FromImage(DB)
Try : SelectedTab.BackColor = Color.White
Catch
End Try
DG.Clear(Color.White)
DG.FillRectangle(New SolidBrush(Color.FromArgb(239, 239, 242)), New Rectangle(0, 0, ItemSize.Height + 4, Height))
DG.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999))
For i = 0 To TabCount - 1
If i = SelectedIndex Then
Dim x4 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
Dim _mblend As New ColorBlend()
_mblend.Colors = {Color.FromArgb(0, 122, 204), Color.FromArgb(0, 122, 204), Color.FromArgb(0, 122, 204)}
_mblend.Positions = {0.0F, 0.5F, 1.0F}
Dim _lgBrush As New LinearGradientBrush(x4, Color.Black, Color.Black, 90.0F)
_lgBrush.InterpolationColors = _mblend
DG.FillRectangle(_lgBrush, x4)
DG.DrawRectangle(New Pen(Color.FromArgb(170, 187, 204)), x4)
DG.SmoothingMode = SmoothingMode.HighQuality
Dim p() As Point = {New Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20),
New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)}
DG.FillPolygon(Brushes.White, p)
DG.DrawPolygon(New Pen(Color.FromArgb(170, 187, 204)), p)
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) Is Nothing Then
DG.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x4.Location.X + 8, x4.Location.Y + 6))
DG.DrawString(" " & TabPages(i).Text, Font, Brushes.Black, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
DG.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Brushes.Black, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
DG.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Brushes.Black, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
'DG.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular, Brushes.White, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center}))
End If
DG.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x4.Location.X - 1, x4.Location.Y - 1), New Point(x4.Location.X, x4.Location.Y))
DG.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x4.Location.X - 1, x4.Bottom - 1), New Point(x4.Location.X, x4.Bottom))
Else
Dim x4 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height + 1))
DG.FillRectangle(New SolidBrush(Color.FromArgb(239, 239, 242)), x4)
DG.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(x4.Right, x4.Top), New Point(x4.Right, x4.Bottom))
If ImageList Is Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) Is Nothing Then
DG.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x4.Location.X + 8, x4.Location.Y + 6))
DG.DrawString("" & TabPages(i).Text, Font, Brushes.Black, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
DG.DrawString(TabPages(i).Text, Font, Brushes.White, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
DG.DrawString(TabPages(i).Text, Font, Brushes.Black, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
DG.DrawString(TabPages(i).Text, Font, Brushes.White, x4, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
End If
Next
e.Graphics.DrawImage(DB.Clone, 0, 0)
DG.Dispose() : DB.Dispose()
End Sub
Private Sub InitializeComponent()
Me.SuspendLayout()
Me.ResumeLayout(False)
End Sub
End Class
Tags: Visual Basic