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.

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

12.8 PictureBoxes<br />

A picture box (class PictureBox) displays an image. The image, set by an object of class<br />

Image, can be in a bitmap, a GIF (Graphics Interchange Format), a JPEG (Joint Pho<strong>to</strong>graphic<br />

Expert Group), icon or metafile format. (Images and multimedia are discussed in<br />

Chapter 16, Graphics and Multimedia.)<br />

The Image property specifies the image that is displayed, and the SizeMode property<br />

indicates how the image is displayed (Normal, StretchImage, Au<strong>to</strong>size or Center-<br />

Image). Figure 12.27 describes important properties and events of class PictureBox.<br />

The program in Fig. 12.28 uses PictureBox picImage <strong>to</strong> display one of three<br />

bitmap images—image0, image1 or image2. These images are located in the direc<strong>to</strong>ry<br />

images (in the bin/images direc<strong>to</strong>ry of our project), where the executable file is also<br />

located. Whenever a user clicks picImage, the image changes. The Label<br />

(named lblPrompt) at the <strong>to</strong>p of the form displays the text Click On Picture Box <strong>to</strong><br />

View Images.<br />

PictureBox<br />

properties and events Description / Delegate and Event Arguments<br />

Common Properties<br />

Image Sets the image <strong>to</strong> display in the PictureBox.<br />

SizeMode Enumeration that controls image sizing and positioning. Values are<br />

Normal (default), StretchImage, Au<strong>to</strong>Size and CenterImage.<br />

Normal places image in <strong>to</strong>p-left corner of PictureBox, and<br />

CenterImage puts image in middle (both truncate image if it is <strong>to</strong>o<br />

large). StretchImage resizes image <strong>to</strong> fit in PictureBox.<br />

Au<strong>to</strong>Size resizes PictureBox <strong>to</strong> hold image.<br />

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

Click Generated when user clicks the control. Default event when this control<br />

is double clicked in the designer.<br />

Fig. 12.27 PictureBox properties and events.<br />

1 ' Fig. 12.28: PictureBoxTest.vb<br />

2 ' Using a PictureBox <strong>to</strong> display images.<br />

3<br />

4 Imports System.IO<br />

5 Imports System.Windows.Forms<br />

6<br />

7 Public Class FrmPictureBox<br />

8 Inherits Form<br />

9<br />

10 Private imageNumber As Integer = -1<br />

11<br />

12 ' instructions display label<br />

13 Friend WithEvents lblPrompt As Label<br />

Fig. 12.28 Using a PictureBox <strong>to</strong> display images (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!