Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

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


  1. Click the Close Project command on the File menu, and then create a new Windows
    Forms Application project named My Anchor and Dock.

  2. Display the form.

  3. Click the PictureBox control, and then add a picture box object in the top middle of the form.

  4. Click the Image property in the Properties window, and then click the ellipsis button in
    the second column.
    The Select Resource dialog box appears.

  5. Click the Local Resource radio button, and then click the Import button.

  6. In the Open dialog box, navigate to the C:\Vb10sbs\Chap15 folder.

  7. In the Files Of Type list box, select All Files.

  8. Select Sun .ico, and then click Open.

  9. Click OK in the Select Resource dialog box.


The Sun icon appears in the PictureBox.


  1. Set the SizeMode property on the PictureBox to StretchImage.

  2. Use the TextBox control to create a text box object.

  3. Set the Multiline property for the text box object to True so that you can resize the
    object appropriately.

  4. Resize the text box object so that it covers most of the bottom half of the form.

  5. Click the Button control, and then add a button object to the lower-right corner of the form.

  6. Set the following properties for the button and text box objects.


Object Property Setting
Button1 Text “Align Now”
TextBox1 Text “Anchor and Dock Samples”
Free download pdf