mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-05 16:06:10 +02:00
* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
2.8 KiB
2.8 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| Define an event in controls | 03/30/2017 |
|
|
d89f1096-8061-42e2-a855-a1f053f1940a |
Defining an Event in Windows Forms Controls
For details about defining custom events, see Events. If you define an event that does not have any associated data, use the base type for event data, xref:System.EventArgs, and use xref:System.EventHandler as the event delegate. All that remains to do is to define an event member and a protected OnEventName method that raises the event.
The following code fragment shows how the FlashTrackBar custom control defines a custom event, ValueChanged. For the complete code for the FlashTrackBar sample, see the How to: Create a Windows Forms Control That Shows Progress.
Option Explicit
Option Strict
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class FlashTrackBar
Inherits Control
' The event does not have any data, so EventHandler is adequate
' as the event delegate.
' Define the event member using the event keyword.
' In this case, for efficiency, the event is defined
' using the event property construct.
Public Event ValueChanged As EventHandler
' The protected method that raises the ValueChanged
' event when the value has actually
' changed. Derived controls can override this method.
Protected Overridable Sub OnValueChanged(e As EventArgs)
RaiseEvent ValueChanged(Me, e)
End Sub
End Class
using System;
using System.Windows.Forms;
using System.Drawing;
public class FlashTrackBar : Control {
// The event does not have any data, so EventHandler is adequate
// as the event delegate.
private EventHandler onValueChanged;
// Define the event member using the event keyword.
// In this case, for efficiency, the event is defined
// using the event property construct.
public event EventHandler ValueChanged {
add {
onValueChanged += value;
}
remove {
onValueChanged -= value;
}
}
// The protected method that raises the ValueChanged
// event when the value has actually
// changed. Derived controls can override this method.
protected virtual void OnValueChanged(EventArgs e)
{
onValueChanged?.Invoke(this, e);
}
}