Control.OnMouseHover Method
Raises the MouseHover event.
[Visual Basic] Protected Overridable Sub OnMouseHover( _ ByVal e As EventArgs _ ) [C#] protected virtual void OnMouseHover( EventArgs e ); [C++] protected: virtual void OnMouseHover( EventArgs* e ); [JScript] protected function OnMouseHover( e : EventArgs );
Parameters
- e
- An EventArgs that contains the event data.
Remarks
Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.
The OnMouseHover method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
Notes to Inheritors: When overriding OnMouseHover in a derived class, be sure to call the base class's OnMouseHover method so that registered delegates receive the event.
Example
[Visual Basic, C#] The following code example demonstrates how to override the OnMouseHover and the OnMouseMove methods in a derived class. To run the example, paste the following code in a new form and paste this class, forming the same file, after the form. Add a button of type FunButton to the form.
[Visual Basic] Public Class FunButton Inherits Button Protected Overrides Sub OnMouseHover(ByVal e As System.EventArgs) ' Get the font size in Points, add one to the ' size, and reset the button's font to the larger ' size. Dim fontSize As Single = Font.SizeInPoints fontSize += 1 Dim buttonSize As System.Drawing.Size = Size Me.Font = New System.Drawing.Font _ (Font.FontFamily, fontSize, Font.Style) ' Increase the size width and height of the button ' by 5 points each. Size = New System.Drawing.Size _ (Size.Width + 5, Size.Height + 5) ' Call myBase.OnMouseHover to activate the delegate. MyBase.OnMouseHover(e) End Sub Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) ' Make the curser the Hand curser when the mouse moves ' over the button. Cursor = Cursors.Hand ' Call MyBase.OnMouseHover to activate the delegate. MyBase.OnMouseHover(e) End Sub [C#] public class FunButton: Button { protected override void OnMouseHover(System.EventArgs e) { // Get the font size in Points, add one to the // size, and reset the button's font to the larger // size. float fontSize = Font.SizeInPoints; fontSize += 1; System.Drawing.Size buttonSize = Size; this.Font = new System.Drawing.Font( Font.FontFamily, fontSize, Font.Style); // Increase the size width and height of the button // by 5 points each. Size = new System.Drawing.Size(Size.Width+5, Size.Height+5); // Call myBase.OnMouseHover to activate the delegate. base.OnMouseHover(e); } protected override void OnMouseMove(MouseEventArgs e) { // Make the curser the Hand curser when the mouse moves // over the button. Cursor = Cursors.Hand; // Call MyBase.OnMouseHover to activate the delegate. base.OnMouseHover(e); }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
Control Class | Control Members | System.Windows.Forms Namespace | MouseHover