Handle MouseOver Event of Tabs

code3software
code3software used Ask the Experts™
on
I have a cutom control that inherits the TabControl.  The code for my custom control is attached in the code portion of this post.  I was wondering if there was a way I could handle a mouseover event for the tabs.  
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Text
Imports System.Windows.Forms
 
Public Class GradientTabControl
    Inherits TabControl
 
    Private pgStartColor As System.Drawing.Color
    Private pgEndColor As System.Drawing.Color
    Private unselectedTabStartColor As System.Drawing.Color
    Private unselectedTabEndColor As System.Drawing.Color
    Private selectedTabStartColor As System.Drawing.Color
    Private selectedTabEndColor As System.Drawing.Color
 
 
    Public Sub New()
        Call InitializeComponent()
 
        Me.pgStartColor = Color.White
        Me.pgEndColor = Color.White
        Me.unselectedTabStartColor = Color.White
        Me.unselectedTabEndColor = Color.White
        Me.selectedTabEndColor = Color.Black
        Me.selectedTabStartColor = Color.Black
 
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    End Sub
 
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
 
        '   Paint the Background
        Me.PaintTransparentBackground(e.Graphics, Me.ClientRectangle)
 
        '   Paint all the tabs
        If Me.TabCount > 0 Then
            For index As Integer = 0 To Me.TabCount - 1
                Me.PaintTab(e, index)
                Me.DrawTabPage(index)
            Next
        End If
 
        '   paint a border round the pagebox
        '   We can't make the border disappear so have to do it like this.
        If Me.TabCount > 0 Then
 
            Dim borderRect As Rectangle = Me.TabPages(0).Bounds
            If Me.SelectedTab IsNot Nothing Then
                borderRect = Me.SelectedTab.Bounds
            End If
 
            borderRect.Inflate(1, 1)
            ControlPaint.DrawBorder(e.Graphics, borderRect, Me.pgEndColor, ButtonBorderStyle.Solid)
 
        End If
 
        '   repaint the bit where the selected tab is
        Select Case Me.SelectedIndex
            Case -1
            Case 0
                Dim selrect As Rectangle = Me.GetTabRect(Me.SelectedIndex)
                Dim selrectRight As Integer = selrect.Right
                e.Graphics.DrawLine(New Pen(Me.pgStartColor), selrect.Left, selrect.Bottom + 1, selrectRight, selrect.Bottom + 1)
            Case Else
                Dim selrect As Rectangle = Me.GetTabRect(Me.SelectedIndex)
                Dim selrectRight As Integer = selrect.Right
                e.Graphics.DrawLine(New Pen(Me.pgStartColor), selrect.Left, selrect.Bottom + 1, selrectRight, selrect.Bottom + 1)
        End Select
 
    End Sub
 
 
    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        If Me.DesignMode Then
            Dim backBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Me.Bounds, SystemColors.ControlLightLight, SystemColors.ControlLight, Drawing2D.LinearGradientMode.Vertical)
            pevent.Graphics.FillRectangle(backBrush, Me.Bounds)
            backBrush.Dispose()
        Else
            Me.PaintTransparentBackground(pevent.Graphics, Me.ClientRectangle)
        End If
    End Sub
 
 
    Protected Sub PaintTransparentBackground(ByVal g As Graphics, ByVal clipRect As Rectangle)
        If (Me.Parent IsNot Nothing) Then
            clipRect.Offset(Me.Location)
            Dim e As PaintEventArgs = New PaintEventArgs(g, clipRect)
            Dim state As GraphicsState = g.Save
            g.SmoothingMode = SmoothingMode.HighSpeed
            Try
                g.TranslateTransform(CType(-Me.Location.X, Single), CType(-Me.Location.Y, Single))
                Me.InvokePaintBackground(Me.Parent, e)
                Me.InvokePaint(Me.Parent, e)
 
            Finally
                g.Restore(state)
                clipRect.Offset(-Me.Location.X, -Me.Location.Y)
            End Try
        Else
            Dim backBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Me.Bounds, SystemColors.ControlLightLight, SystemColors.ControlLight, Drawing2D.LinearGradientMode.Vertical)
            g.FillRectangle(backBrush, Me.Bounds)
            backBrush.Dispose()
        End If
    End Sub
 
 
    Private Sub DrawTabPage(ByVal TabPageIndex As Integer)
        Dim r As New RectangleF(0, 0, Me.TabPages(TabPageIndex).Width, Me.TabPages(TabPageIndex).Height)
 
        Dim gradBrush As New LinearGradientBrush(r, Me.pgStartColor, Me.pgEndColor, LinearGradientMode.Vertical)
 
        Dim bmp As New Bitmap(Me.TabPages(TabPageIndex).Width, Me.TabPages(TabPageIndex).Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.FillRectangle(gradBrush, r)
        Me.TabPages(TabPageIndex).BackgroundImage = bmp
        Me.TabPages(TabPageIndex).BackgroundImageLayout = ImageLayout.Stretch
 
    End Sub
 
 
    Private Sub PaintTab(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal i As Integer)
        Dim r As RectangleF = Me.GetTabRect(i)
 
        Dim path As System.Drawing.Drawing2D.GraphicsPath = Me.GetPath(i, r)
        Me.PaintTabBackground(e.Graphics, i, path)
        Me.PaintTabBorder(e.Graphics, i, path)
        Me.PaintTabText(e.Graphics, i)
    End Sub
 
 
    Private Sub PaintTabBackground(ByVal graph As System.Drawing.Graphics, ByVal index As Integer, ByVal path As System.Drawing.Drawing2D.GraphicsPath)
        Dim rect1 As RectangleF = Me.GetTabRect(index)
 
        rect1.Width -= 3
        rect1.Height *= 0.75
 
        Dim rect2 As RectangleF = Me.GetTabRect(index)
 
        rect2.Width -= 4
        rect2.Y += rect1.Height * 0.75
        rect2.X += 1
        rect2.Height *= 0.75
 
 
        If rect1.Height > 1 AndAlso rect1.Width > 1 Then
            Dim buttonBrush1 As System.Drawing.Brush = _
                New System.Drawing.Drawing2D.LinearGradientBrush(rect1, Me.unselectedTabStartColor, Me.unselectedTabEndColor, LinearGradientMode.Vertical)
 
            Dim buttonBrush2 As System.Drawing.Brush = _
                New System.Drawing.Drawing2D.LinearGradientBrush(rect2, Me.selectedTabEndColor, Me.selectedTabStartColor, LinearGradientMode.Vertical)
 
            If index = Me.SelectedIndex Then
                buttonBrush1 = New System.Drawing.Drawing2D.LinearGradientBrush(rect1, Me.selectedTabStartColor, Me.selectedTabEndColor, LinearGradientMode.Vertical)
                buttonBrush2 = New System.Drawing.Drawing2D.LinearGradientBrush(rect2, Me.selectedTabEndColor, Me.selectedTabStartColor, LinearGradientMode.Vertical)
 
            End If
 
            graph.FillPath(buttonBrush1, path)
            graph.FillRectangle(buttonBrush2, rect2)
            buttonBrush1.Dispose()
            buttonBrush2.Dispose()
        End If
 
    End Sub
 
 
    Private Sub PaintTabBorder(ByVal graph As System.Drawing.Graphics, ByVal index As Integer, ByVal path As System.Drawing.Drawing2D.GraphicsPath)
        Dim borderPen As New Pen(Color.Black)
        If index = Me.SelectedIndex Then
            borderPen = New Pen(Me.selectedTabEndColor)
        End If
 
        graph.DrawPath(borderPen, path)
        borderPen.Dispose()
    End Sub
 
 
    Private Sub PaintTabText(ByVal graph As System.Drawing.Graphics, ByVal index As Integer)
        Dim rect As Rectangle = Me.GetTabRect(index)
        rect.Width -= 3
 
        Dim tabtext As String = Me.TabPages(index).Text
 
        Dim format As New System.Drawing.StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        format.Trimming = StringTrimming.EllipsisCharacter
 
        Dim forebrush As Brush = Nothing
 
        If Me.TabPages(index).Enabled = False Then
            forebrush = SystemBrushes.ControlDark
        Else
            forebrush = SystemBrushes.ControlText
        End If
 
        Dim tabFont As Font = Me.Font
        If index = Me.SelectedIndex Then
            tabFont = New Font(Me.Font, FontStyle.Bold)
        End If
 
        graph.DrawString(tabtext, tabFont, forebrush, rect, format)
 
    End Sub
 
 
    Private Function GetPath(ByVal index As Integer, ByVal r As RectangleF) As System.Drawing.Drawing2D.GraphicsPath
        Dim path As New System.Drawing.Drawing2D.GraphicsPath()
        path.Reset()
 
        Dim RectRadius As Integer = 7
 
        Dim rect As Rectangle = Me.GetTabRect(index)
        rect.Width -= 5
        rect.X += 1
 
        path.StartFigure()
 
        'path.AddArc(r, 180, 90)
        'path.AddArc(r, 270, 90)
 
        path.AddArc(rect.X, rect.Y, RectRadius * 2, RectRadius * 2, 180, 90)
        path.AddArc(rect.X + rect.Width - RectRadius * 2, rect.Y, RectRadius * 2, RectRadius * 2, 270, 90)
        'path.AddLine(rect.Right, rect.Top - 10, rect.Right, rect.Bottom)
        path.AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom)
 
        path.CloseFigure()
 
        Return path
    End Function
 
 
#Region "   Properties"
    Public Property PageStartColor() As System.Drawing.Color
        Get
            Return pgStartColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            pgStartColor = value
            Me.Invalidate()
        End Set
    End Property
 
 
    Public Property PageEndColor() As System.Drawing.Color
        Get
            Return pgEndColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            pgEndColor = value
            Me.Invalidate()
        End Set
    End Property
 
 
    Public Property SelTabStartColor() As System.Drawing.Color
        Get
            Return selectedTabStartColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            selectedTabStartColor = value
            Me.Invalidate()
        End Set
    End Property
 
 
    Public Property SelTabEndColor() As System.Drawing.Color
        Get
            Return selectedTabEndColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            selectedTabEndColor = value
            Me.Invalidate()
        End Set
    End Property
 
 
    Public Property unSelTabStartColor() As System.Drawing.Color
        Get
            Return unselectedTabStartColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            unselectedTabStartColor = value
            Me.Invalidate()
        End Set
    End Property
 
 
    Public Property unSelTabEndColor() As System.Drawing.Color
        Get
            Return unselectedTabEndColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            unselectedTabEndColor = value
            Me.Invalidate()
        End Set
    End Property
 
#End Region
End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Since you have inherited the TabControl Class, you should be able to handle its
MouseHover event.
I have included an example with an object of your Class called GTCObject

Sub GTCObject_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles GTCObject.MouseHover
     ..... do something
End Sub

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Do you mean MouseMove()?...so you know which Tab the user is moving over at the top of the TabControl?
Public Class GradientTabControl
    Inherits TabControl
 
    Private Sub GradientTabControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        For i As Integer = 0 To Me.TabPages.Count - 1
            If Me.GetTabRect(i).Contains(e.Location) Then
                Dim tp As TabPage = Me.TabPages(i)
                Debug.Print("Over Tab Index #" & i & " called '" & tp.Text & "'")
                Exit For
            End If
        Next
    End Sub
 
End Class

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial