30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

510 Graphical User Interface Concepts: Part 1 Chapter 12<br />

To respond <strong>to</strong> the user’s clicks, the program must handle the Click event. Inside the<br />

event handler, picImage_Click, we use an Integer (imageNumber) <strong>to</strong> s<strong>to</strong>re the<br />

image we want <strong>to</strong> display. We then set the Image property of picImage <strong>to</strong> an Image<br />

(line 28–30). Although class Image is discussed in Chapter 16, Graphics and Multimedia,<br />

we now overview method FromFile, which takes a String (the image file) and creates<br />

an Image object.<br />

To find the images, we use class Direc<strong>to</strong>ry (namespace System.IO, specified on<br />

line 4) method GetCurrentDirec<strong>to</strong>ry (line 29). This returns the current direc<strong>to</strong>ry of<br />

the executable file as a String. To access the images subdirec<strong>to</strong>ry, we append<br />

“\images\” and the file name <strong>to</strong> the name of the current direc<strong>to</strong>ry. We use image-<br />

Number <strong>to</strong> append the proper number, enabling us <strong>to</strong> load either image0, image1 or<br />

image2. The value of Integer imageNumber stays between 0 and 2 because of the<br />

modulus calculation in line 25. Finally, we append ".bmp" <strong>to</strong> the filename. Thus, if we<br />

want <strong>to</strong> load image0, the String becomes “CurrentDir\images\image0.bmp”,<br />

where CurrentDir is the direc<strong>to</strong>ry of the executable.<br />

12.9 Mouse-Event Handling<br />

This section explains the handling of mouse events, such as clicks, presses and moves,<br />

which are generated when the mouse interacts with a control. Mouse events can be handled<br />

for any control that derives from class System.Windows.Forms.Control. Mouseevent<br />

information is passed through class MouseEventArgs, and the delegate used <strong>to</strong><br />

create mouse-event handlers is MouseEventHandler. Each mouse-event-handling<br />

method requires an Object and a MouseEventArgs object as arguments. For example,<br />

the Click event, which we covered earlier, uses delegate EventHandler and event arguments<br />

EventArgs.<br />

Class MouseEventArgs contains information related <strong>to</strong> the mouse event, such as<br />

the x- and y-coordinates of the mouse pointer, the mouse but<strong>to</strong>n pressed Right, Left or<br />

Middle), the number of times the mouse was clicked and the number of notches through<br />

which the mouse wheel turned. Note that the x- and y-coordinates of the MouseEventArgs<br />

object are relative <strong>to</strong> the control that generated the event. Point (0,0) represents the<br />

upper-left corner of the control. Several mouse events are described in Fig. 12.29.<br />

Mouse Events, Delegates and Event Arguments<br />

Mouse Events (Delegate EventHandler, event arguments EventArgs)<br />

MouseEnter Generated if the mouse cursor enters the area of the control.<br />

MouseLeave Generated if the mouse cursor leaves the area of the control.<br />

Mouse Events (Delegate MouseEventHandler, event arguments MouseEventArgs)<br />

MouseDown Generated if the mouse but<strong>to</strong>n is pressed while its cursor is over the<br />

area of the control.<br />

MouseHover Generated if the mouse cursor hovers over the area of the control.<br />

Fig. 12.29 Mouse events, delegates and event arguments (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!