Capture Input From A Custom Dialog

You typically use dialog boxes in Excel to gather input from the user. The input you capture from a user can be anything from determining which button was pressed to actual values typed by a user. You can capture the user input from the dialog box so you can return the appropriate responses by using the UserForm events. For example, when the user clicks an OK CommandButton control you use a CommandButton_Click subroutine to indicate what steps to perform.

Excel considers every user interaction that occurs on a dialog box, such as scrolling through a list of items, selecting an OK button, or typing text in a text box, as an event. Each UserForm control has several different events that you can capture. The most common event that occurs is the Click event. This event occurs each time a user clicks a control.

To make code interact with the UserForm, you need to create procedures that execute when specific events occur.

Each UserForm you create has two elements: the graphical layout window and a code window. The graphical layout window is the location where you add controls that display on the dialog box. See the section "Create a Custom Dialog Box" for more information on designing custom dialog boxes. Each UserForm also has a code window that contains all UserForm-specific code and that you use to create the event procedures for each control. You can create event code for a specific control on the code window by double-clicking the control. By default, the Visual Basic Editor creates a Click event for the control when you click it. If a Click event already exists, the Visual Basic Editor simply displays the code window.

CAPTURE INPUT FROM A CUSTOM DIALOG BOX

CAPTURE INPUT FROM A CUSTOM DIALOG BOX

□ Create a UserForm within the appropriate project.

0 On the Toolbox, click the ListBox control icon ([#]).

—D Click the CommandButton control icon (Q).

□ Create a UserForm within the appropriate project.

Note: See the section "Create a Custom Dialog Box" for information on creating UserForms.

0 On the Toolbox, click the ListBox control icon ([#]).

0 Drag g] to the UserForm.

—D Click the CommandButton control icon (Q).

0 DragS to the UserForm.

—0 In the Properties window, type "OK" as the Caption property value for the CommandButton control.

■ The text on the CommandButton changes to reflect the value of the caption property.

'—0 Double-click the CommandButton object.

CUSTOMIZING DIALOG BOXES, MENUS, AND TOOLBARS

ra The Click event occurs when the user clicks a control or a value in a control with the mouse button. For most controls you can write a procedure to handle the Click event, the most commonly captured event for dialog boxes, by simply placing the event name after the control name. All event-handling procedures require that you place an underscore character between the control name and the event name.

Example:

Sub CommandButton1_Click()

If you need to capture the Click event to determine the page

or tab selected with a

MultiPage or TabStrip control, the procedure also includes

an index parameter

value that specifies the index to the page or tab.

Example:

Sub MultiPage1_Click(1)

With the MultiPage and TabStrip controls, you need to create a separate procedure to handle the selection of each page or tab by using the corresponding index value.

Besides actually clicking a control with the mouse, a Click event also occurs when you press Enter and a control has focus, when you press the accelerator key that corresponds to the control, or when you press Spacebar and a CommandButton has focus.

■ The code window for the UserForm displays.

-■ Excel creates a subroutine called CommandButtonl _Click() on the code window.

—O Type UserSelection: = ListBoxl.Value, replacing UserSelection with the name of the global variable used to capture user input.

I Type Unload SampleDialogBox, replacing SampleDialogBox with the name of the UserForm.

■ The code window for the UserForm displays.

-■ Excel creates a subroutine called CommandButtonl _Click() on the code window.

—O Type UserSelection: = ListBoxl.Value, replacing UserSelection with the name of the global variable used to capture user input.

I Type Unload SampleDialogBox, replacing SampleDialogBox with the name of the UserForm.

CONTINUED

0 0

Responses

Post a comment