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 paste to copy a group of controls. For example, suppose you want to make name, street, city, state, and ZIP code TextBoxes but you also want Label controls to the left of the TextBoxes. First create the name Label and TextBox, set their properties, and position them so their baselines are lined up and the Label is to the left of the TextBox as desired. Click and drag to select both controls and then press Ctrl+C to copy them both. Now when you press Ctrl+V, the designer makes a copy of the Label and the TextBox. The copies have aligned baselines and the Label is to the left of the TextBox as in the originals. The new controls are even both selected so you can use the mouse to grab them both and drag them into position.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.169.197