Adding a Control to a Form

When you add a UserForm to your project, another window immediately appears. This window, called the Toolbox, is where you'll find the intrinsic, or default, ActiveX controls that are available for you to design your user interface (see Figure 3-7).

Placing an ActiveX control on a form is simply a matter of clicking the control in the Toolbox and then clicking the area of the UserForm where you want to place the control. Each time you drop a new control onto a form, VBA automatically provides a default name and index number for the control. For example, the first TextBox control will be named TextBoxl by default. You can rename controls by modifying their Name property.

When you click the ActiveX control you want, the cursor becomes a plus (+) sign, and the icon of the control appears to the lower right of the plus sign when the cursor is moved over the UserForm. The middle of the plus sign signifies the upper-left corner (the Left and Top properties) of the control. By default, the control will snap to the closest grid point displayed on your UserForm. Grid density can be controlled in the General tab of the Tools/Options dialog.

For example, pick the TextBox control, as shown in Figure 3-8. Place the control in the upper-left corner of the UserForm, similar to what you see in Figure 3-9.

Figure 3-7. The

ActiveX Toolbox

Figure 3-7. The

ActiveX Toolbox

Figure 3-8. The Toolbox with IntelliSense displayed
Figure 3-9. A UserForm with a TextBox control

Now create a dialog box interface that looks something like the example depicted in Figure 3-10. (Don't worry about making it look exactly like the figure; you just need to have the same controls on a form for this example.) Table 3-1 lists the controls and their respective property settings.

Figure 3-10. A completed UserForm
Table 3-1. Class Controls and Their Property Settings

Control Class

Name

Caption

Label

Label1

X Coordinate

Label

Label2

Y Coordinate

Label

Label3

Z Coordinate

CommandButton

cmdPick

< Pick ...

CommandButton

cmdOK

OK

TextBox

txtX

TextBox

txtY

TextBox

txtZ

Next you'll enter some code and see how to make this dialog box interface work. First, you need to open the UserForm code module by selecting View > Code or pressing F7. Or you may start by double-clicking the < Pick ... command button and entering the code listed here:

Private Sub cmdPick_Click() Dim Point As Variant On Error Resume Next 'hide the UserForm UserForm1.Hide 'ask user to select a point

Point = ThisDrawing.Utility.GetPoint(, "Select a point") If Err Then Exit Sub 'assign values to appropriate textbox txtX = Point(0): txtY = Point(1): txtZ = Point(2) 'redisplay the UserForm UserForm1.Show End Sub

This is all the code you need to make this example work. Execute this example by selecting Run > Run Sub/UserForm or by pressing F5. Alternatively, you can run the example by clicking the Run toolbar button as shown in Figure 3-11.

This final bit of code allows you to exit your application gracefully:

Private Sub cmdOK_Click()

Unload Me End Sub

Figure 3-11. The

VBAIDE Run, Pause, and Stop buttons

Was this article helpful?

0 0

Post a comment