Files
docs-desktop/dotnet-desktop-guide/net/winforms/input-mouse/how-to-simulate-events.md
T
Andy De GeorgeandGenevieve Warren 184e6c9a0c Initial publish of WinForms for .NET 5. (#96)
* Migrate inprogress winforms contnet from docs

* Fix preview note

* Fix vb code

* Minor fixes to keyboard articles

* Minor adjustment to overview

* Test redirects for 4.0 -> 5.0

* adjust links

* Add mouse input section winforms (#81)

* Update projects to .NET 5

* Update keyboard desc

* Finish mouse events

* Add remaining mouse articles;code

* finish mouse input

* minor fixes

* Remove branch restriction (#82)

* Update build-validation.yml (#84)

* Update build-validation.yml (#85)

* Fix vb proj

* Add WinForms tutorial (#91)

* redirect between overview

* New create an app tutorial

* Fix markdown

* Fix preserve view setting

* Add forms articles (#93)

* Add forms articles

* Fix warnings

* Winforms publish (#95)

* Prep TOC for publish

* Updated date

* Automatic how-to topic type

* Update desc/headers

* Fix links

* Fix build errors

* Add preview note

* Update toc landing page

* Convert old style project files

* update download links

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* Adjust number key

* Update dotnet-desktop-guide/net/winforms/overview/index.md

* Try toc position task via bookmark

* Improve images

* Remove sentence

* File redirects

Co-authored-by: Genevieve Warren <[email protected]>
2020-10-28 10:44:16 -07:00

4.1 KiB

title, description, ms.date, dev_langs, helpviewer_keywords
title description ms.date dev_langs helpviewer_keywords
How to simulate mouse events Learn how to simulate mouse events in Windows Forms for .NET. 10/26/2020
csharp
vb
mouse [Windows Forms], event simulation
user input [Windows Forms], simulating
SendKeys [Windows Forms], using

How to simulate mouse events (Windows Forms .NET)

Simulating mouse events in Windows Forms isn't as straight forward as simulating keyboard events. Windows Forms doesn't provide a helper class to move the mouse and invoke mouse-click actions. The only option for controlling the mouse is to use native Windows methods. If you're working with a custom control or a form, you can simulate a mouse event, but you can't directly control the mouse.

[!INCLUDE desktop guide under construction]

Events

Most events have a corresponding method that invokes them, named in the pattern of On followed by EventName, such as OnMouseMove. This option is only possible within custom controls or forms, because these methods are protected and can't be accessed from outside the context of the control or form. The disadvantage to using a method such as OnMouseMove is that it doesn't actually control the mouse or interact with the control, it simply raises the associated event. For example, if you wanted to simulate hovering over an item in a xref:System.Windows.Forms.ListBox, OnMouseMove and the ListBox doesn't visually react with a highlighted item under the cursor.

These protected methods are available to simulate mouse events.

  • OnMouseDown
  • OnMouseEnter
  • OnMouseHover
  • OnMouseLeave
  • OnMouseMove
  • OnMouseUp
  • OnMouseWheel
  • OnMouseClick
  • OnMouseDoubleClick

For more information about these events, see Using mouse events (Windows Forms .NET)

Invoke a click

Considering most controls do something when clicked, like a button calling user code, or checkbox change its checked state, Windows Forms provides an easy way to trigger the click. Some controls, such as a combobox, don't do anything special when clicked and simulating a click has no effect on the control.

PerformClick

The xref:System.Windows.Forms.IButtonControl?displayProperty=nameWithType interface provides the xref:System.Windows.Forms.IButtonControl.PerformClick%2A method which simulates a click on the control. Both the xref:System.Windows.Forms.Button?displayProperty=nameWithType and xref:System.Windows.Forms.LinkLabel?displayProperty=nameWithType controls implement this interface.

:::code language="csharp" source="snippets/how-to-simulate-events/csharp/Form1.cs" id="PerformClick"::: :::code language="vb" source="snippets/how-to-simulate-events/vb/Form1.vb" id="PerformClick":::

InvokeClick

With a form a custom control, use the xref:System.Windows.Forms.Control.InvokeOnClick%2A method to simulate a mouse click. This is a protected method that can only be called from within the form or a derived custom control.

For example, the following code clicks a checkbox from button1.

:::code language="csharp" source="snippets/how-to-simulate-events/csharp/Form1.cs" id="ClickCheckbox"::: :::code language="vb" source="snippets/how-to-simulate-events/vb/Form1.vb" id="ClickCheckbox":::

Use native Windows methods

Windows provides methods you can call to simulate mouse movements and clicks such as User32.dll SendInput and User32.dll SetCursorPos. The following example moves the mouse cursor to the center of a control:

:::code language="csharp" source="snippets/how-to-simulate-events/csharp/Form2.cs" id="MoveCursor"::: :::code language="vb" source="snippets/how-to-simulate-events/vb/Form2.vb" id="MoveCursor":::

See also