Visual Basic Concepts

Creating a Control Array at Design Time

See Also

There are three ways to create a control array at design time:

To add a control array element by changing its name

  1. Draw the controls you want to be in the control array. (The controls must all be of the same type.) Decide which control will become the first element in the array.

  2. Select one of the controls and change its Name setting to the Name setting for the first element in the array.

  3. When you type an existing name for a control in the array, Visual Basic displays a dialog box asking you to confirm that you want to create a control array. Choose Yes to confirm the action.

For example, if the name of the first element in a control array is cmdCtlArr, you would choose a command button to add to the array and then set its name to cmdCtlArr. The message "You already have a control named 'cmdCtlArr.' Do you want to create a control array?" is displayed. Choose Yes to confirm the operation.

Controls added this way share only their Name property and control type; all other properties remain the same as when the control was originally drawn.

To add a control array element by copying an existing control

  1. Draw a control in the control array.

  2. While the control has the focus, choose Copy from the Edit menu.

  3. From the Edit menu, choose Paste. Visual Basic displays a dialog box asking you to confirm that you want to create a control array. Choose Yes to confirm the action.

    This control is assigned an index value of 1. The first control you drew has a value of 0.

The index value of each new array element corresponds to the order in which the element was added to the control array. When controls are added this way, most of the visual properties, such as height, width, and color, are copied from the first control in the control array to the new controls.