Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 14 Managing Windows Forms and Controls at Run Time 369


Your form looks similar to this:


  1. Double-click the Align Now button to open the Button1_Click event procedure in the
    Code Editor.

  2. Type the following program code:


PictureBox1.Dock = DockStyle.Top
TextBox1.Anchor = AnchorStyles.Bottom Or _
AnchorStyles.Left Or AnchorStyles.Right Or _
AnchorStyles.Top
Button1.Anchor = AnchorStyles.Bottom Or _
AnchorStyles.Right
When this event procedure is executed, the Dock property of the PictureBox1 object
is used to dock the picture box to the top of the form. This forces the top edge of the
picture box object to touch and adhere to the top edge of the form—much as the
Visual Studio docking feature works in the IDE. The only surprising behavior here is that
the picture box object is also resized so that its sides adhere to the left and right edges
of the form.
Next, the Anchor property for the TextBox1 and Button1 objects is used. The Anchor
property maintains the current distance from the specified edges of the form, even
if the form is resized. Note that the Anchor property maintains the object’s current
distance from the specified edges—it doesn’t attach the object to the specified edges
unless it’s already there. In this example, I specify that the TextBox1 object should
be anchored to all four edges of the form (bottom, left, right, and top). I use the Or
operator to combine my edge selections. I anchor the Button1 object to the bottom
and right edges of the form.


  1. Save the project, and then specify the C:\Vb10sbs\Chap14 folder as the location.

Free download pdf