Files
docs-desktop/dotnet-desktop-guide/net/winforms/forms/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.6 KiB
Raw Blame History

title, description, ms.date, ms.topic, helpviewer_keywords
title description ms.date ms.topic helpviewer_keywords
Events Overview A brief overview about events with .NET Windows Forms. 10/26/2020 overview
Windows Forms, event handling
events [Windows Forms], about events
delegates [Windows Forms], multicast
delegates [Windows Forms], events and
multicast event delegates
Windows Forms controls, events

Events overview (Windows Forms .NET)

An event is an action that you can respond to, or "handle," in code. Events can be generated by a user action, such as clicking the mouse or pressing a key, by program code, or by the system.

[!INCLUDE desktop guide under construction]

Event-driven applications execute code in response to an event. Each form and control exposes a predefined set of events that you can program against. If one of these events occurs and there's code an associated event handler, that code is invoked.

The types of events raised by an object vary, but many types are common to most controls. For example, most objects will handle a xref:System.Windows.Forms.Control.Click event. If a user clicks a form, code in the form's xref:System.Windows.Forms.Control.Click event handler is executed.

Note

Many events occur in conjunction with other events. For example, in the course of the xref:System.Windows.Forms.Control.DoubleClick event occurring, the xref:System.Windows.Forms.Control.MouseDown, xref:System.Windows.Forms.Control.MouseUp, and xref:System.Windows.Forms.Control.Click events occur.

For information about how to raise and consume an event, see Handling and raising events.

Delegates and their role

Delegates are classes commonly used within .NET to build event-handling mechanisms. Delegates roughly equate to function pointers, commonly used in Visual C++ and other object-oriented languages. Unlike function pointers however, delegates are object-oriented, type-safe, and secure. Also, where a function pointer contains only a reference to a particular function, a delegate consists of a reference to an object, and references to one or more methods within the object.

This event model uses delegates to bind events to the methods that are used to handle them. The delegate enables other classes to register for event notification by specifying a handler method. When the event occurs, the delegate calls the bound method. For more information about how to define delegates, see Handling and raising events.

Delegates can be bound to a single method or to multiple methods, referred to as multicasting. When creating a delegate for an event, you typically create a multicast event. A rare exception might be an event that results in a specific procedure (such as displaying a dialog box) that wouldn't logically repeat multiple times per event. For information about how to create a multicast delegate, see How to combine delegates (Multicast Delegates).

A multicast delegate maintains an invocation list of the methods it's bound to. The multicast delegate supports a xref:System.Delegate.Combine%2A method to add a method to the invocation list and a xref:System.Delegate.Remove%2A method to remove it.

When an event is recorded by the application, the control raises the event by invoking the delegate for that event. The delegate in turn calls the bound method. In the most common case (a multicast delegate), the delegate calls each bound method in the invocation list in turn, which provides a one-to-many notification. This strategy means that the control doesn't need to maintain a list of target objects for event notification—the delegate handles all registration and notification.

Delegates also enable multiple events to be bound to the same method, allowing a many-to-one notification. For example, a button-click event and a menu-commandclick event can both invoke the same delegate, which then calls a single method to handle these separate events the same way.

The binding mechanism used with delegates is dynamic: a delegate can be bound at run-time to any method whose signature matches that of the event handler. With this feature, you can set up or change the bound method depending on a condition and to dynamically attach an event handler to a control.

See also