Sie sind auf Seite 1von 8

Module 2: Working With Controls 1

Overview

 Creating an Event Handler for a Control


 Using Windows Forms Controls
 Using Dialogs
 Validating User Input
 Creating Controls at Run Time
 Creating Menus

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction When you design the user interface (UI) and develop the code that operates
behind the UI of an application, you will need to work with controls and their
events, properties, and methods to meet the design requirements that you have
been given.
This module covers how to create event procedures (handlers) in your
application that will run in response to user actions. You will learn how to add
programming logic to the event procedures of a control, how to use the
Windows Forms intrinsic controls, dialog boxes, and menus, and how to
validate the data entered by users of your application.
Objectives After completing this module, you will be able to:
 Create an event handler for a control.
 Select and use the appropriate controls in a Windows Forms application.
 Use dialog boxes in a Windows Forms application.
 Validate user input in a Windows Forms application.
 Add controls to a form at run time.
 Create and use menus in a Windows Forms application.
2 Module 2: Working With Controls

Lesson: Creating an Event Handler for a Control

 Event Model in the .NET Framework


 What Are Delegates?
 What Is an Event Handler?
 How to Create Handlers for Control Events
 How to Add and Remove Event Handlers at Run Time
 Practice: Creating an Event Handler for a Control

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction In the .NET Framework, an event is a message sent by an object to signal the
occurrence of an action that is either user invoked by a user or
programmatically. Each event has a sender that raises the event and a receiver
that handles the event.
In this lesson, you will learn about events and the ways in which events can be
handled in your application. You will then learn how to create procedures that
handle events and how to add and remove event handlers at run time.
Lesson objectives After completing this lesson, you will be able to:
 Describe the event model in the .NET Framework.
 Create and use event handlers.
 Create event procedures using the Handles and WithEvents keywords.
 Add and remove handles from event procedures at run time.
Module 2: Working With Controls 3

Event Model in the .NET Framework

Button1 this.button1.Click += new


System.EventHandler(this.button1_Click);

private void button1_Click(object


sender, System.EventArgs e)
Invokes the {
delegate
}

Delegate calls the


associated procedure
Delegate

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction In the .NET Framework, an event is used to signal the occurrence of an action.
For example, this action could be user invoked, such as the Click event of a
Button control, or the event could be raised programmatically to signal the end
of a long computation. The object that raises (triggers) the event is referred to as
the event sender. The procedure that handles the event is referred to as the event
receiver. In either case, the sender does not know which object or method will
respond to the events that it raises. Therefore, it is necessary to have a
component that links the event sender with the event receiver. The .NET
Framework uses a Delegate type to work as a function pointer between the
sender and the event receiver. In most cases, the .NET Framework creates the
delegate and takes care of the details for you. However, you can create your
own delegates for the cases where you want an event to call different event
handlers under different circumstances.
Delegate class Delegates are objects that you can use to call the methods of other objects.
Delegates are useful in situations where you need an intermediary between a
calling procedure and the procedure being called. You create a Delegate class
that is used as the base class for a Delegate type. Within the Delegate class, you
create the methods that are used to respond to the event or events that the
delegate handles.
4 Module 2: Working With Controls

What Are Delegates?

 Delegate
 Binds events to methods
 Can be bound to single or multiple methods
 When an event is recorded by an application
 The control raises the event by invoking the delegate
for the event
 The delegate in turn calls the bound method

public
public delegate
delegate void
void AlarmEventHandler(object
AlarmEventHandler(object
sender,
sender, AlarmEventArgs
AlarmEventArgs e);
e);

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction In the .NET Framework, delegates are used to hold a reference to the method
that will handle an event. For example, an event occurs when a user clicks a
button. The button raises a click event, but does not know what behavior you,
the programmer, want to occur when the button is clicked, so the button has a
delegate member to which you assign your own method for handling the event.
You can use the same infrastructure that is used by the .NET Framework to
create your own delegates.
Definition A delegate is a data structure, derived from the Delegate Class, which refers to
a static method or to a class instance and an instance method of that class.
Using delegates is useful when your application must perform an action by
calling a method but you do not know what that action will be.
Delegates allow you to specify at runtime the method to be invoked. Delegates
are object-oriented, type-safe, and secure.
Event delegate By convention, event delegates in the .NET Framework have two parameters,
declaration the source that raised the event and the data for the event. The following
example shows an event delegate declaration:
public delegate void AlarmEventHandler(object sender,
AlarmEventArgs e);

Event delegates are multicast, which means that they can hold references to
more than one event handling method. Delegates allow for flexibility and fine-
grain control in event handling. A delegate acts as an event dispatcher for the
class that raises the event by maintaining a list of registered event handlers for
the event. For more information on Delegates, see Delegate Class in the
Visual Studio .NET help.
Module 2: Working With Controls 5

What Is an Event Handler?


 Event Handlers
 Methods bound to an event
 When the event is raised, the code within the event
handler is executed
 Two Event Arguments with Event Handlers
 An object representing the object that raised the event
 An event object containing any event-specific
information
private
private void
void button1_Click(object
button1_Click(object sender,
sender,
System.EventArgs
System.EventArgs e)
e)
{{

}}
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Introduction Functionality is added to controls by raising and consuming events. Before your
application can respond to an event, you must create an event handler. The
event handler (event procedure) contains the program logic that runs when the
event is raised.
Definition An event handler is a method (generally a sub procedure) that is bound to an
event. When the event is raised, the code within the event handler runs. You can
use the same event handler to handle more than one event. For example, you
can create a single event handler to handle events of a button and a menu item
that are used for the same purpose. Similarly, if you have a group of
RadioButton controls on a form, you could create a single event handler and
have each control's Click event bound to the single event handler.
Example of event The following code example is an event handler for the Click event of a button.
handler
private void button1_Click(object sender, System.EventArgs e)
{

The following code example shows how you can use a single event handler to
handle events for multiple controls.
6 Module 2: Working With Controls

// inside the Windows Form Designer generated code region



this.button1.Click += new
System.EventHandler(this.button1_Click);

// add the button2.click event to button1_click handler


this.button2.Click += new
System.EventHandler(this.button1_Click);

private void button1_Click(object sender, System.EventArgs e)


{

Event handler Each event handler provides two parameters that allow you to handle the event
parameters properly.
 The first parameter (Sender in the previous code example), provides a
reference to the object that raised the event. It specifies the source that
raised the event.
 The second parameter (e in the previous code example), passes an object
specific to the event being handled. This parameter contains all of the data
that is required to handle the event.
Module 2: Working With Controls 7

How to Create Handlers for Control Events

 Use WithEvents keyword to declare object variables


that will be used with the Handles statement
 Use the Handles keyword at the end of the procedure
declaration

private
private void
void button1_Click(object
button1_Click(object sender,
sender,

System.EventArgs
System.EventArgs e)
e)
{{
MessageBox.Show("MyHandler
MessageBox.Show("MyHandler received
received the
the event");
event");

}}

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction C# .NET uses the 'System.EventHandler += new ' syntax to define an event
handler. The standard way to create an event handler in C# .NET is through the
Properties pane of the control.
Procedure To create an event procedure for a control:
1. In the Designer view, click the control that you want to create an event
handler for.
2. In the Properties pane, click Event (the button displaying the lightning bolt).
3. Double-click the event that you want to handle. Code will be added to the
Code view.
4. Add program logic to the event handler procedure using the supplied
arguments. The following code provides an example:
private void button1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("MyHandler received the event");

}
8 Module 2: Working With Controls

How to Add and Remove Event Handlers at Run Time

 To associate an event with an event handler at run time


 Use the AddHandler statement

this.button2.Click
this.button2.Click +=
+= new
new
System.EventHandler(this.button1_Click);
System.EventHandler(this.button1_Click);

 To remove the association of an event with an event


handler at run time
 Use the RemoveHandler statement
this.button2.Click
this.button2.Click -=
-= new
new
System.EventHandler(this.button1_Click);
System.EventHandler(this.button1_Click);

*****************************ILLEGAL FOR NON-TRAINER USE******************************


Introduction In C# .NET, you can add and remove event handlers at run time by using the
System.EventHandler += new and System.EventHandler -= new
syntax.
Procedure: Adding To add event handlers by using System.EventHandler += new syntax:
event handlers using
AddHandler 1. Use the System.EventHandler += new syntax to specify the name of
the event sender and receiver, as shown in the following code example:
// inside the Windows Form Designer generated code region

this.button1.Click += new
System.EventHandler(this.button1_Click);

// add the button2.click event to button1_click handler


this.button2.Click += new
System.EventHandler(this.button1_Click);

private void button1_Click(object sender, System.EventArgs


e)
{

Procedure: Removing To remove event handlers by using System.EventHandler -= new syntax:


event handlers using
RemoveHandler Use the System.EventHandler -= new syntax to specify the name of the
event sender and receiver.
// remove the button2.click event from button1_click
handler
this.button2.Click -= new
System.EventHandler(this.button1_Click);

Das könnte Ihnen auch gefallen