In C#, an event handler is a method that contains code that will be executed when a specific event occurs in the application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections triggered by user interface controls. Events are user actions such as keypresses, mouse clicks, mouse movements, and system-generated messages. In C#, events are connected to handlers by the event delegate.
Delegates with Events
The C # event model follows the “publish-subscribe” pattern. In this pattern, one class (publisher) dispatches events and another class (subscriber) receives events. Two things are required to raise an event and respond to it: the delegate that connects the event to its handler and the class that holds the event data. Events are encapsulated delegates that provide an additional layer of security. Delegated events are supported by C# and .NET. When the state of the application changes, events and delegates send notifications to the client application.
Event Declaration Syntax:
public event EventHandler MyEvent;
Declaration of Delegate using Event:
The following example demonstrates how to declare a delegate name “MyEventHandler”. For an Event declaration in a class, the delegate’s event type must be declared initially.
public delegate void MyEventHandler(object sender, MyEventArgs e);
The following section contains C# programs on events, event handling, mouse handling events, predicates, actions, elapsed events, and clones. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.
Here is the listing of C# programming examples on Events:
- C# Programs on Mouse Handling Events
- C# Programs on Displaying the Times
- C# Programs on Predicates and Actions
- C# Programs on Elapsed Events and Clones
1. C# Programs on Mouse Handling Events
Program | Description |
---|---|
Perform Addition with MOUSEUP Event in C# | C# Program to Perform Addition with MOUSEUP Event |
Perform Subtraction with Key Up Event in C# | C# Program to Perform Subtraction with Key Up Event |
Add Two TimeSpan in C# | C# Program to Add Two TimeSpan |
2. C# Programs on Displaying the Times
Program | Description |
---|---|
Local Time Program in C# | C# Program to Get the Local Time |
Universal Time in C# | C# Program to Get the Universal Time |
DayLight Saving Information in C# | C# Program to Get the DayLight Saving Information |
Check if ENTER key is Pressed or Not in C# | C# Program to Check if ENTER Key is Pressed or Not |
3. C# Programs on Predicates and Actions
Program | Description |
---|---|
Predicate in C# | C# Program to Illustrate Predicate |
Actions in C# | C# Program to Illustrate Actions |
Progress Bar Control in C# | C# Program to Create a Progress Bar Control |
Text Box in C# | C# Program to Create Input Box and Display the Text |
Radio Button in C# | C# Program to Create a Radio Button |
4. C# Programs on Elapsed Events and Clones
Program | Description |
---|---|
Elapsed Event in C# | C# Program to Illustrate Elapsed Event |
Clone() in C# | C# Program to Demonstrate Use of Clone |
Trigger in C# | C# Program to Demonstrate Trigger |