Expanding and Shrinking Objects While a Program Is Running
In addition to maintaining a Top property and a Left property, Visual Basic maintains a Height property and a Width property for most objects on a form. You can use these properties in clever ways to expand and shrink objects while a program is running. The following exercise shows you how to do it.
Expand a picture box at run time
On the File menu, click the Close Project command.
Create a new project named My Zoom In.
Display the form, click the PictureBox control in the Toolbox, and then draw a small picture box object near the upper-left corner of the form.
Set the following properties for the picture box and the form. When you set the properties for the picture box, note the current values in the Height and Width properties within the Size property. (You can set these at design time, too.)
Object | Property | Setting |
PictureBox1 | Image SizeMode | “c:\vb05sbs\chap15\earth.ico” StretchImage |
Form1 | Text | “Approaching Earth” |
Double-click the PictureBox1 object on the form.
The PictureBox1_Click event procedure appears in the Code Editor.
Type the following program code in the PictureBox1_Click event procedure:
PictureBox1.Height = PictureBox1.Height + 15 PictureBox1.Width = PictureBox1.Width + 15
These two lines increase the height and width of the Earth icon by 15 pixels each time the user clicks the picture box. If you stretch your imagination a little, watching the effect makes you feel like you're approaching Earth in a spaceship.
Click the Save All button, and then save the project in the c:\vb05sbs\chap15 folder.
TIP
The complete Zoom In program is located in the c:\vb05sbs\chap15\zoom in folder.
Click the Start Debugging button to run the program.
The Earth icon appears alone on the form, as shown here:
Click the Earth icon several times to expand it on the screen.
After 10 or 11 clicks, your screen looks similar to this:
When you get close enough to establish a standard orbit, click the Close button to quit the program.
The program stops, and the development environment returns.