COPYING CONTROLS

A particularly useful technique for building a series of similar controls is to build one and then use copy and paste to make others.

For example, to build the name, street, city, state, and ZIP code TextBoxes described in the previous section, you could start by adding the name TextBox to the form. Next, set all of the properties that you want the control copies to share. For example, you may want to adjust the TextBox’s width, set its MaxLength property to 20, and set its Anchor property to Top, Left, Right so it resizes horizontally when its container resizes. Now select the control on the designer and press Ctrl+C to copy it. Then press Ctrl+V repeatedly to make copies for the other controls. Drag the controls into position and you have quickly built all of the controls with their shared properties already set.

CONTAINER CONFUSION
When you paste a copied control, the new control is placed inside whatever container is currently selected on the form. This can be confusing if you quickly copy and paste a container. For example, suppose you want to make three GroupBoxes. You build one and size it the way you want it. Then you press Ctrl+C, Ctrl+V, Ctrl+V. The first GroupBox is copied and the first copy is pasted inside the original GroupBox. Then the second copy is placed inside the first copy. The result is somewhat confusing and you’ll probably need to drag the copies out onto the form before you can place them where you want.

You can also use copy and ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.