Software Development

Move and resize controls on a VB.NET form

When you need to be able move and appropriately resize the controls on a Visual Basic .NET form, Irina Medvinskaya suggests that you use the <i>Anchor</i> property. Find out why in this tip

There may be times when you need to move and appropriately resize the controls on a form. In this quick tip, I show how to set the controls in VB.NET forms so you can properly resize the controls based on the size of the form.

Using the Anchor property

A simple way to make sure the form's controls are moved and/or resized in accordance to the container form is to use the control's Anchor property. In general, this property allows the control to maintain a constant relationship with the side(s) of the form specified in the Anchor property. For instance, setting an Anchor property of a TextBox control on a form to Left allows the control to always be the same distance from the Left edge of the form when it's resized. You can Anchor a control to the Right, Left, Top, Bottom, or any combination of the above. If you set the Anchor property to all four sides, it allows a control to be resized with the form.

For example, add a TextBox control to your form, set its Multiline property to True, and its Anchor property to all four sides. See Figure 1. The result of debugging the form will look like Figure 2. Now resize the form to make it bigger, and the text control will be resized together with the form as in Figure 3.

Note: In this example, setting the TextBox control's Multiline property to True allows its height to be adjusted when the form's height changes. If the TextBox control's Multiline property is set to False, only the control's width will get adjusted when the form's size is changed—even when the TextBox is anchored to all four sides of the form.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

6 comments
t.henry
t.henry

hi to everybody i wanted to now how to move controls (like labels) in forms i have a little problem in location property thanks

techie000
techie000

Thanks i just resized my controls using the above tip.

sudipta1984born
sudipta1984born

tis very good n come with a time saving procedure. tis work well.

pbhanuchander
pbhanuchander

this works fine when there are few controls, what if if the form has many controls and each are grouped into different group boxes. If we set the anchor property on all four sides then the controsl may overlap each other.

vanars
vanars

Does anyone know how to do this? VB Help says to change the policy file. Where is this file. Can read only be disabled for all applications?

Tony Hopkinson
Tony Hopkinson

If the component/class designer defined them as read only that means if you hacked past this, you are more than likely to break the component, even if you got away with it, upgrade to the next version will cause you a whole bucket load of problems. If you need to do this either the component is badly designed, you are using the wrong one, or you've misunderstood something.

Editor's Picks