How to: Create OnEnter Functionality

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework does not support the OnEnter and OnLeave methods for controls. However, because the OnMouseMove method is supported, you can use it and the Capture property to determine when the mouse pointer enters or leaves the control.

This example defines a simple custom control, MouseCapture, which is blue when the mouse movements occur inside the control, and light gray when they outside of the control. It determines whether the mouse coordinate is within its ClientRectangle using the OnMouseMove method.

Note that tapping in and out of the control will not change its color. You must drag the mouse, such as for a drag and drop operation.

To create and implement the custom control

  1. Add the MouseCapture custom control to your project.

    Public Class MouseCapture
       Inherits Control
       Public Sub New()
          Me.BackColor = Color.LightGray
       End Sub 'New
       ' If the mouse is over the control, Capture is true. 
       Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
          Me.Capture = Me.ClientRectangle.Contains(e.X, e.Y)
          If Me.Capture Then
             ' Blue indicates inside the control.
             Me.BackColor = Color.Blue
             Me.BackColor = Color.LightGray
          End If
       End Sub 
    End Class
  2. Create an instance of MouseCapture in the form's constructor or for its Load event.

    ' Assumes mc has been delared
    ' for the form as type MouseCapture.
      Dim mc As New MouseCapture()
      mc.Parent = Me
      mc.Bounds = New Rectangle(20, 50, 100, 50)

This example requires references to the following namespaces: