Tip A Check Box or an Option Button

Use option buttons where only one option can be selected at a given time. Use check boxes to have the user select any number of options that apply.

7. Adding a label and a combo box:

■ Click the Label control in the Toolbox.

■ Click the empty space below the frame labeled Computer Type. The Label1 control should appear.

■ Change the Caption property of Label1 to Where Used.

■ Click the ComboBox control in the Toolbox.

■ Click the empty space below the Where Used label and drag the mouse to draw a rectangle. Release the mouse button.

The combo box displays a list of available choices only after you click the down arrow placed at the right of this control. The combo box is sometimes referred to as a drop-down list and is used to save valuable space on the screen. Although the user can only see one element of the list at a given time, the current selection can be quickly changed by clicking on the arrow button.

8. Adding a label, a text box, and a spin button:

■ Click the Label control in the Toolbox.

■ Click on the empty place of the form just below the Where Used combo box. A label control will appear. Change the Caption property for this label to Percent (%) Used.

■ Click the TextBox control in the Toolbox.

■ Click to the right of the Percent (%) Used label control to place a default size text box.

■ Click the SpinButton control in the Toolbox, and then click to the right side of the text box control. A default size spin button will appear. The final result is shown in Figure 10-11.

The spin button has two arrows that are used to increment or decrement a value in a given range. The maximum value is determined by the setting of the Max property, and the minimum value is set with the Min property. The spin button has the same properties as the scroll bar, with two differences: the spin button does not have a scroll box, and it lacks the LargeChange property. A text box is usually placed next to the spin button. This allows the user to enter a value directly into the text box or use the spin buttons to determine the value. If the spin button has to work with the text box, your VBA procedure must ensure that the value of the text box and the spin button are synchronized. In this example, you will use the spin button to indicate the percent of interest that the user has in the selected hardware or software product.

9. Adding command buttons:

■ Double-click the CommandButton control in the Toolbox. Recall that by double-clicking the control in the Toolbox, you indicate that you want to create more than one control using the selected tool.

■ Click in the top right-hand corner of the form. This will cause the CommandButton1 to appear.

■ Click below CommandButton1. CommandButton2 will appear.

■ Change the Caption property of CommandButton1 to OK and CommandButton2 to Cancel.

Most custom forms have two command buttons, OK and Cancel, that enable the user to accept the data entered on the form or dismiss the form. In this example, the OK button will transfer the data entered on the form onto a worksheet. The user will be able to click the Cancel button whenever he is done inputting the data. To make the buttons respond to user actions, you will write appropriate VBA procedures later in this chapter.

10. Adding an image control:

■ Click the Image button in the Toolbox.

■ Click with the mouse below the Cancel button, and drag the mouse to draw a rectangle. Release the mouse button. The final result is shown in Figure 10-11. The form will display a different picture depending on whether the Hardware or Software option button is selected. The images will be loaded by a VBA procedure.

11. Checking the appearance of the form:

■ Click the title bar, or click on any empty area of the form to select it.

■ Press F5 or choose Run | Run Sub/UserForm to display the form as the user will see it.

■ Visual Basic switches to the active sheet in the Microsoft Excel window and displays the custom form you designed. If you forget to select the form, the Macro dialog box will appear. Close the dialog box, and repeat the two previous steps.

■ Click the Close button (x) in the top right-hand corner of the form to close the form and return to the Visual Basic Editor. Recall that the OK and Cancel buttons placed on the form aren't yet functional. They require VBA procedures to make them work.

After you've added controls to the form, use the mouse or Format menu commands to adjust alignment and spacing of the controls.

Tip 10-5: Working with the UserForm Toolbar

The UserForm toolbar contains a number of useful shortcuts for working with forms, such as making controls the same size, centering a control horizontally or vertically, aligning control edges, and grouping and ungrouping controls. To display this toolbar, choose View | Toolbars | UserForm.

0 0

Post a comment