Event Handling

Forms react to events. This is known as event handling . An event is generated due to many different actions, such as when you resize a form, click the form, or move the mouse over the form, as well as when a change in time occurs or another application performs some type of action that causes the current application to be affected.

There are two basic methods of event handling:

  • Using the Handles keyword

  • Attaching a delegate

Chapter 2, "Controls on Forms," discusses the details of triggering events. For our purposes here, we'll examine one of the more common triggering events used in graphical user interface (GUI) designthe MouseDown event, which is triggered when the user clicks a mouse button.

Using the Handles Keyword

The easiest way to handle events is to supply an event-handling method and to use the Handles keyword to specify the event that it handles. For example, this method handles the MouseDown event of the form where the code is located (the MyBase keyword refers to the class on which the form is based):

 Private Sub Form1_MouseDown(ByVal sender As Object, _  ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown     MessageBox.Show(String.Format("X ={0}, Y={1}", e.X, e.Y), _      String.Format("The {0} mouse button hit me at:", e.Button.ToString())) End Sub 

In this example, the MouseDown event triggers the display of the current position of the mouse over the form in a message box, as shown in Figure 1.5.

Figure 1.5. A message box displaying the results of the MouseDown event.

graphics/01fig05.jpg

The MouseEventArgs Type

The MouseDown event example utilizes a derived class, MouseEventArgs , that contains event- related data. This class is derived from the System.Windows.EventArgs type. Table 1.4 lists some of the properties of the MouseEventArgs type.

Table 1.4. The MouseEventArgs Properties

Member

Description

Button

Returns a value of type MouseButtons that specifies which mouse button was pressed.

Clicks

Returns the number of times the mouse button was pressed and released.

Delta

A signed count of the number of detents the mouse wheel has rotated . A detent is one notch of the mouse wheel.

X

The x-coordinate of a mouse click.

Y

The y-coordinate of a mouse click.

The MessageBox Class

The MouseDown example also uses a MessageBox object to display information to the user. This is a useful class to call when performing debugging because you can display a message box showing the data values and details of processing as the code is run.

The MessageBox class is located in the Systems.Windows.Forms namespace and inherits from the Object class. It also includes several Show() options that are unique to its class. Within these are several enumerated arguments, including the values for MessageBoxButtons and MessageBoxIcons .

graphics/note_icon.gif

An enumerated type includes a specific list of values that may be selected, such as an enumerated list for the type "pet" including values of "dog," "cat," "fish," and "bird."


When manipulating instances of the MessageBox class, you should know the enumerated members available in the MessageBoxButtons arguments (see Table 1.5) and the MessageBoxIcons arguments (see Table 1.6).

Table 1.5. The MessageBoxButtons Enumerator List

Member

The Message Box Shows These Buttons

AbortRetryIgnore

Abort, Retry, and Ignore

OK

OK

OKCancel

OK and Cancel

RetryCancel

Retry and Cancel

YesNo

Yes and No

YesNoCancel

Yes, No, and Cancel

Table 1.6. The MessageBoxIcons Enumerator List

Member

The Message Box Contains This Symbol

Asterisk

A lowercase letter i in a circle

Error

A white letter X in a circle with a red background

Exclamation

An exclamation point in a triangle with a yellow background

Hand

A white letter X in a circle with a red background

Information

A lowercase letter i in a circle

None

No symbol

Question

A question mark in a circle

Stop

A white letter X in a circle with a red background

Warning

An exclamation point in a triangle with a yellow background

Attaching a Delegate

Another option for handling an event is to create your own event-handling code and attach the custom function using the AddHandler method. In order to add the same event handling as the previous example, using a delegated function, the following would be added to the form's code:

 Sub Form1_Mousedown(ByVal sender As Object, _  ByVal e As MouseEventArgs)     MessageBox.Show(String.Format("X ={0}, Y={1}", e.X, e.Y), _      String.Format("The {0} mouse button hit me at:", e.Button.ToString())) End Sub 

In order to delegate the MouseClick subroutine to perform event handling, you would also need to add this line to the form's constructor:

 AddHandler Me.MouseDown, AddressOf Me.Form1_MouseDown 

The AddHandler method specifies the delegation of the Me.Form1_MouseDown function to handle MouseDown events on the form. To detach the delegate, you would use the RemoveHandler method, which is similar in syntax to AddHandler . Attaching and detaching a delegate allows you to turn on and off a particular type of event-handling function or to change the event-handling delegate during different parts of your application's operation.

graphics/tip_icon.gif

If you designate multiple event handlers that respond to the same event, they will execute sequentially in the order they are registered.




Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net