Module 2: Working With Controls 1 [624899]

Module 2: Working With Controls 1

Overview
/square6Creating an Event Handler for a Control
/square6Using Windows Forms Controls
/square6Using Dialogs
/square6Validating User Input
/square6Creating Controls at Run Time
/square6Creating Menus

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
When you design the user interface (UI) and develop the code t hat operates
behind the UI of an application, you will need to work with contro ls and their
events, properties, and methods to meet the design requiremen ts that you have
been given.
This module covers how to create event procedures (handlers) i n your
application that will run in response to user actions. You w ill learn how to add
programming logic to the event procedures of a control, how to us e the
Windows Forms intrinsic controls, dialog boxes, and menus , and how to
validate the data entered by users of your application.
After completing this module, you will be able to:
/square6 Create an event handler for a control.
/square6 Select and use the appropriate controls in a Windows Forms appli cation.
/square6 Use dialog boxes in a Windows Forms application.
/square6 Validate user input in a Windows Forms application.
/square6 Add controls to a form at run time.
/square6 Create and use menus in a Windows Forms application.
Introduction
Objectives

2 Module 2: Working With Controls

Lesson: Creating an Event Handler for a Control
/square6Event Model in the .NET Framework
/square6What Are Delegates?
/square6What Is an Event Handler?
/square6How to Create Handlers for Control Events
/square6How to Add and Remove Event Handlers at Run Time
/square6Practice: Creating an Event Handler for a Control

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
In the .NET Framework, an event is a message sent by an object to sig nal the
occurrence of an action that is either user invoked by a user or
programmatically. Each event has a sender that raises the ev ent 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 .
After completing this lesson, you will be able to:
/square6 Describe the event model in the .NET Framework.
/square6 Create and use event handlers.
/square6 Create event procedures using the Handles and WithEvents keywords.
/square6 Add and remove handles from event procedures at run time.
Introduction
Lesson objectives

Module 2: Working With Controls 3

Event Model in the .NET Framework
Button1
Invokes the
delegate this.button1.Click += new
System.EventHandler(this.button1_Click);
private void button1_Click(object
sender, System.EventArgs e)
{

}
Delegate calls the
associated procedure Delegate Delegate Delegate this.button1.Click += new
System.EventHandler(this.button1_Click);

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
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 si gnal the end
of a long computation. The object that raises (triggers) the even t 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 obj ect 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. Th e .NET
Framework uses a Delegate type to work as a function pointer between the
sender and the event receiver. In most cases, the .NET Framewo rk creates the
delegate and takes care of the details for you. However, you c an create your
own delegates for the cases where you want an event to call diff erent event
handlers under different circumstances.
Delegates are objects that you can use to call the methods of oth er objects.
Delegates are useful in situations where you need an interme diary between a
calling procedure and the procedure being called. You crea te 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 tha t the
delegate handles. Introduction
Dele gate class

4 Module 2: Working With Controls

What Are Delegates?
public delegate void AlarmEventHandler(object
sender, AlarmEventArgs e); public delegate void AlarmEventHandler(object
sender, AlarmEventArgs e); /square6Delegate
/circle6Binds events to methods
/circle6Can be bound to single or multiple methods
/square6When an event is recorded by an application
/circle6The control raises the event by invoking the delega te
for the event
/circle6The delegate in turn calls the bound method

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
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 butto n has a
delegate member to which you assign your own method for handlin g the event.
You can use the same infrastructure that is used by the .NET Frame work to
create your own delegates.
A delegate is a data structure, derived from the Delegate Class, which refe rs to
a static method or to a class instance and an instance method of tha t 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.
By convention, event delegates in the .NET Framework have two p arameters,
the source that raised the event and the data for the event. The f ollowing
example shows an event delegate declaration:
public delegate void AlarmEventHandler(object sende r,
AlarmEventArgs e);

Event delegates are multicast, which means that they can hold re ferences to
more than one event handling method. Delegates allow for flexi bility and fine-
grain control in event handling. A delegate acts as an event di spatcher for the
class that raises the event by maintaining a list of registe red event handlers for
the event. For more information on Delegates, see “Delegat e Class” in the
Visual Studio .NET help.
Introduction
Definition
Event delegate
declaration

Module 2: Working With Controls 5

What Is an Event Handler?
/square6Event Handlers
/circle6Methods bound to an event
/circle6When the event is raised, the code within the event
handler is executed
/square6Two Event Arguments with Event Handlers
/circle6An object representing the object that raised the e vent
/circle6An event object containing any event-specific
information
private void button1_Click(object sender,
System.EventArgs e)
{
}private void button1_Click(object sender,
System.EventArgs e)
{
}

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
Functionality is added to controls by raising and consuming e vents. Before your
application can respond to an event, you must create an event h andler. The
event handler (event procedure) contains the program logic t hat runs when the
event is raised.
An event handler is a method (generally a sub procedure) that is b ound to an
event. When the event is raised, the code within the event han dler 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 a nd a menu item
that are used for the same purpose. Similarly, if you have a grou p of
RadioButton controls on a form, you could create a single event handler an d
have each control's Click event bound to the single event handler.
The following code example is an event handler for the Click event of a button.
private void button1_Click(object sender, System.Ev entArgs e)
{

}

The following code example shows how you can use a single event handler to
handle events for multiple controls. Introduction
Definition
Example of event
handle r

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 han dler
this.button2.Click += new
System.EventHandler(this.button1_Click);

private void button1_Click(object sender, System.Ev entArgs e)
{

}

Each event handler provides two parameters that allow you to han dle the event
properly.
/square6 The first parameter ( Sender in the previous code example), provides a
reference to the object that raised the event. It specifies t he source that
raised the event.
/square6 The second parameter ( e in the previous code example), passes an object
specific to the event being handled. This parameter contains al l of the data
that is required to handle the event.
Event handler
parameters

Module 2: Working With Controls 7

How to Create Handlers for Control Events
private void button1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("MyHandler received the event");
}private void button1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("MyHandler received the event");
}/square6Use WithEvents keyword to declare object variables
that will be used with the Handles statement
/square6Use the Handles keyword at the end of the procedure
declaration

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
C# .NET uses the 'System.EventHandler += new …' syntax to defin e an event
handler. The standard way to create an event handler in C# .NET i s through the
Properties pane of the control.
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 add ed to the
Code view.
4. Add program logic to the event handler procedure using the sup plied
arguments. The following code provides an example:
private void button1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("MyHandler received the event");

}
Introduction
Procedure

8 Module 2: Working With Controls

How to Add and Remove Event Handlers at Run Time
/square6To associate an event with an event handler at run time
/circle6Use the AddHandler statement
/square6To remove the association of an event with an event
handler at run time
/circle6Use the RemoveHandler statement
this.button2.Click -= new
System.EventHandler(this.button1_Click); this.button2.Click -= new
System.EventHandler(this.button1_Click); this.button2.Click += new
System.EventHandler(this.button1_Click); this.button2.Click += new
System.EventHandler(this.button1_Click);

***************************** ILLEGAL FOR NON -TRAINER USE ******************************
In C# .NET, you can add and remove event handlers at run time by us ing the
System.EventHandler += new … and System.EventHandler -= new
…syntax.
To add event handlers by using System.EventHandler += new … syntax:
1. Use the System.EventHandler += new … syntax to specify the name of
the event sender and receiver, as shown in the following code exam ple:
// 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 han dler
this.button2.Click += new
System.EventHandler(this.button1_Click);

private void button1_Click(object sender, System.Ev entArgs
e)
{

}

To remove event handlers by using System.EventHandler -= new … syntax:
• Use the System.EventHandler -= new … syntax to specify the name of the
event sender and receiver.
// remove the button2.click event from button1_clic k
handler
this.button2.Click -= new
System.EventHandler(this.button1_Click);

Introduction
Procedure: Adding
event handlers using
AddHandle r
Procedure: Removing
event handlers using
RemoveHandle r

Similar Posts