Export (0) Print
Expand All

Control.MouseHover Event

Occurs when the mouse pointer rests on the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Event MouseHover As EventHandler
Dim instance As Control 
Dim handler As EventHandler 

AddHandler instance.MouseHover, handler

A typical use of MouseHover is to display a tool tip when the mouse pauses on a control within a specified area around the control (the "hover rectangle"). The pause required for this event to be raised is specified in milliseconds by the MouseHoverTime property.

The MouseHover event is defined and detected in connection with the MouseHoverSize and MouseHoverTime properties.

Mouse events occur in the following order:

  1. MouseEnter

  2. MouseMove

  3. MouseHover / MouseDown / MouseWheel

  4. MouseUp

  5. MouseLeave


The following events are not raised for the TabControl class unless there is at least one TabPage in the TabControl.TabPages collection: Click, DoubleClick, MouseDown, MouseUp, MouseHover, MouseEnter, MouseLeave and MouseMove. If there is at least one TabPage in the collection, and the user interacts with the tab control's header (where the TabPage names appear), the TabControl raises the appropriate event. However, if the user interaction is within the client area of the tab page, the TabPage raises the appropriate event.

For more information about handling events, see Consuming Events.

The following code example demonstrates using different mouse events to draw the path of the mouse on a Panel. A line segment is added to the GraphicsPath for each MouseMove and MouseDown events that occur. To update the graphics, the Invalidate method is called for the Panel on each MouseDown and MouseUp event. In addition, the graphic path is scrolled up or down when the MouseWheel event occurs. Additional mouse events, like MouseHover, are identified on screen as well. Also displayed on the screen is additional information about the mouse from the SystemInformation class.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace MouseEvent
    ' Summary description for Form1. 
    Public NotInheritable Class Form1
        Inherits System.Windows.Forms.Form

        Friend WithEvents panel1 As System.Windows.Forms.Panel
        Private label1 As System.Windows.Forms.Label
        Private label2 As System.Windows.Forms.Label
        Private label3 As System.Windows.Forms.Label
        Private label4 As System.Windows.Forms.Label
        Private label5 As System.Windows.Forms.Label
        Private label6 As System.Windows.Forms.Label
        Private label7 As System.Windows.Forms.Label
        Private label8 As System.Windows.Forms.Label
        Private label9 As System.Windows.Forms.Label
        Friend WithEvents clearButton As System.Windows.Forms.Button
        Private mousePath As System.Drawing.Drawing2D.GraphicsPath
        Private groupBox1 As System.Windows.Forms.GroupBox

        Private fontSize As Integer = 20

        <System.STAThread()>  _
        Public Shared Sub Main()
            System.Windows.Forms.Application.Run(New Form1())
        End Sub 'Main

        Public Sub New()

            mousePath = New System.Drawing.Drawing2D.GraphicsPath()

            Me.panel1 = New System.Windows.Forms.Panel()
            Me.label1 = New System.Windows.Forms.Label()
            Me.clearButton = New System.Windows.Forms.Button()
            Me.label2 = New System.Windows.Forms.Label()
            Me.label3 = New System.Windows.Forms.Label()
            Me.label4 = New System.Windows.Forms.Label()
            Me.label5 = New System.Windows.Forms.Label()
            Me.label6 = New System.Windows.Forms.Label()
            Me.label7 = New System.Windows.Forms.Label()
            Me.label8 = New System.Windows.Forms.Label()
            Me.label9 = New System.Windows.Forms.Label()
            Me.groupBox1 = New System.Windows.Forms.GroupBox()

            ' Mouse Events Label 
            Me.label1.Location = New System.Drawing.Point(24, 504) 
            Me.label1.Size = New System.Drawing.Size(392, 23) 
            ' DoubleClickSize Label 
            Me.label2.AutoSize = True  
            Me.label2.Location = New System.Drawing.Point(24, 48) 
            Me.label2.Size = New System.Drawing.Size(35, 13) 
            ' DoubleClickTime Label 
            Me.label3.AutoSize = True  
            Me.label3.Location = New System.Drawing.Point(24, 72) 
            Me.label3.Size = New System.Drawing.Size(35, 13) 
            ' MousePresent Label 
            Me.label4.AutoSize = True  
            Me.label4.Location = New System.Drawing.Point(24, 96) 
            Me.label4.Size = New System.Drawing.Size(35, 13) 
            ' MouseButtons Label 
            Me.label5.AutoSize = True  
            Me.label5.Location = New System.Drawing.Point(24, 120) 
            Me.label5.Size = New System.Drawing.Size(35, 13) 
            ' MouseButtonsSwapped Label 
            Me.label6.AutoSize = True  
            Me.label6.Location = New System.Drawing.Point(320, 48) 
            Me.label6.Size = New System.Drawing.Size(35, 13) 
            ' MouseWheelPresent Label 
            Me.label7.AutoSize = True  
            Me.label7.Location = New System.Drawing.Point(320, 72) 
            Me.label7.Size = New System.Drawing.Size(35, 13) 
            ' MouseWheelScrollLines Label 
            Me.label8.AutoSize = True  
            Me.label8.Location = New System.Drawing.Point(320, 96) 
            Me.label8.Size = New System.Drawing.Size(35, 13) 
            ' NativeMouseWheelSupport Label 
            Me.label9.AutoSize = True  
            Me.label9.Location = New System.Drawing.Point(320, 120) 
            Me.label9.Size = New System.Drawing.Size(35, 13) 

            ' Mouse Panel 
            Me.panel1.Anchor = System.Windows.Forms.AnchorStyles.Top Or _
                                    System.Windows.Forms.AnchorStyles.Left Or _
            Me.panel1.BackColor = System.Drawing.SystemColors.ControlDark
            Me.panel1.Location = New System.Drawing.Point(16, 160)
            Me.panel1.Size = New System.Drawing.Size(664, 320)

            ' Clear Button 
            Me.clearButton.Anchor = System.Windows.Forms.AnchorStyles.Top Or _
            Me.clearButton.Location = New System.Drawing.Point(592, 504)
            Me.clearButton.TabIndex = 1
            Me.clearButton.Text = "Clear" 

            ' GroupBox 
            Me.groupBox1.Anchor = System.Windows.Forms.AnchorStyles.Top Or _
                                  System.Windows.Forms.AnchorStyles.Left Or _
            Me.groupBox1.Location = New System.Drawing.Point(16, 24)
            Me.groupBox1.Size = New System.Drawing.Size(664, 128)
            Me.groupBox1.Text = "System.Windows.Forms.SystemInformation" 

            ' Set up how the form should be displayed and add the controls to the form. 
            Me.ClientSize = New System.Drawing.Size(696, 534)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label9, _
                            Me.label8, Me.label7, Me.label6, Me.label5, Me.label4, _
                            Me.label3, Me.label2, Me.clearButton, Me.panel1, Me.label1, Me.groupBox1})

            Me.Text = "Mouse Event Example" 

            ' Display information about the system mouse.
            label2.Text = "SystemInformation.DoubleClickSize: " + SystemInformation.DoubleClickSize.ToString()
            label3.Text = "SystemInformation.DoubleClickTime: " + SystemInformation.DoubleClickTime.ToString()
            label4.Text = "SystemInformation.MousePresent: " + SystemInformation.MousePresent.ToString()
            label5.Text = "SystemInformation.MouseButtons: " + SystemInformation.MouseButtons.ToString()
            label6.Text = "SystemInformation.MouseButtonsSwapped: " + SystemInformation.MouseButtonsSwapped.ToString()
            label7.Text = "SystemInformation.MouseWheelPresent: " + SystemInformation.MouseWheelPresent.ToString()
            label8.Text = "SystemInformation.MouseWheelScrollLines: " + SystemInformation.MouseWheelScrollLines.ToString()
            label9.Text = "SystemInformation.NativeMouseWheelSupport: " + SystemInformation.NativeMouseWheelSupport.ToString()
        End Sub 'New 

        Private Sub panel1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseDown
            ' Update the mouse path with the mouse information 
            Dim mouseDownLocation As New Point(e.X, e.Y)

            Dim eventString As String = Nothing 
            Select Case e.Button
                Case MouseButtons.Left
                    eventString = "L" 
                Case MouseButtons.Right
                    eventString = "R" 
                Case MouseButtons.Middle
                    eventString = "M" 
                Case MouseButtons.XButton1
                    eventString = "X1" 
                Case MouseButtons.XButton2
                    eventString = "X2" 
                Case MouseButtons.None:
                     eventString = Nothing               
            End Select 

            If (eventString IsNot Nothing) Then
                mousePath.AddString(eventString, FontFamily.GenericSerif, CInt(FontStyle.Bold), fontSize, mouseDownLocation, StringFormat.GenericDefault)
                mousePath.AddLine(mouseDownLocation, mouseDownLocation)
            End If

        End Sub  

        Private Sub panel1_MouseEnter(sender As Object, e As System.EventArgs) Handles panel1.MouseEnter
            ' Update the mouse event label to indicate the MouseEnter event occurred.
            label1.Text = sender.GetType().ToString() + ": MouseEnter" 
        End Sub 

        Private Sub panel1_MouseHover(sender As Object, e As System.EventArgs) Handles panel1.MouseHover
            ' Update the mouse event label to indicate the MouseHover event occurred.
            label1.Text = sender.GetType().ToString() + ": MouseHover" 
        End Sub 

        Private Sub panel1_MouseLeave(sender As Object, e As System.EventArgs) Handles panel1.MouseLeave
            ' Update the mouse event label to indicate the MouseLeave event occurred.
            label1.Text = sender.GetType().ToString() + ": MouseLeave" 
        End Sub 

        Private Sub panel1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseMove
            ' Update the mouse path that is drawn onto the Panel. 
            Dim mouseX As Integer = e.X
            Dim mouseY As Integer = e.Y

            mousePath.AddLine(mouseX, mouseY, mouseX, mouseY)
        End Sub 
        Private Sub panel1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseWheel
            ' Update the drawing based upon the mouse wheel scrolling. 
            Dim numberOfTextLinesToMove As Integer = CInt(e.Delta * SystemInformation.MouseWheelScrollLines / 120) 
            Dim numberOfPixelsToMove As Integer = numberOfTextLinesToMove * fontSize

            If numberOfPixelsToMove <> 0 Then 
                Dim translateMatrix As New System.Drawing.Drawing2D.Matrix()
                translateMatrix.Translate(0, numberOfPixelsToMove)
            End If

        End Sub 
        Private Sub panel1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles panel1.MouseUp
            Dim mouseUpLocation As New System.Drawing.Point(e.X, e.Y)

            ' Show the number of clicks in the path graphic. 
            Dim numberOfClicks As Integer = e.Clicks
            mousePath.AddString("     " + numberOfClicks.ToString(), _
                                FontFamily.GenericSerif, CInt(FontStyle.Bold), _
                                fontSize, mouseUpLocation, StringFormat.GenericDefault)

        End Sub 

        Private Sub panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles panel1.Paint
            ' Perform the painting of the Panel.
            e.Graphics.DrawPath(System.Drawing.Pens.DarkRed, mousePath)
        End Sub 

        Private Sub clearButton_Click(sender As Object, e As System.EventArgs) Handles clearButton.Click
            ' Clear the Panel display.
            mousePath = New System.Drawing.Drawing2D.GraphicsPath()
        End Sub 

    End Class 'Form1
End Namespace

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2014 Microsoft