368 Part III Designing the User Interface
two- dimensional thinking or have the time to run the program repeatedly to verify the
placement of your objects.
Fortunately, Visual Basic contains several property settings that you can use to organize
objects on the form at run time. These include the Anchor property, which forces an object
on the form to remain at a constant distance from the specified edges of the form, and the
Dock property, which forces an object to remain attached to one edge of the form. You can
use the Anchor and Dock properties at design time, but I find that they’re also very helpful
for programmatically aligning objects at run time. The following exercise shows how these
properties work.
Anchor and dock objects at run time
- Click the Close Project command on the File menu, and then create a new Windows
Forms Application project named My Anchor and Dock. - Display the form.
- Click the PictureBox control, and then add a picture box object in the top middle of the form.
- Click the Image property in the Properties window, and then click the ellipsis button in
the second column.
The Select Resource dialog box appears. - Click the Local Resource radio button, and then click the Import button.
- In the Open dialog box, navigate to the C:\Vb10sbs\Chap15 folder.
- In the Files Of Type list box, select All Files.
- Select Sun .ico, and then click Open.
- Click OK in the Select Resource dialog box.
The Sun icon appears in the PictureBox.
- Set the SizeMode property on the PictureBox to StretchImage.
- Use the TextBox control to create a text box object.
- Set the Multiline property for the text box object to True so that you can resize the
object appropriately. - Resize the text box object so that it covers most of the bottom half of the form.
- Click the Button control, and then add a button object to the lower-right corner of the form.
- Set the following properties for the button and text box objects.
Object Property Setting
Button1 Text “Align Now”
TextBox1 Text “Anchor and Dock Samples”