Excel AppNote: "Introduction to Custom Dialog Boxes" (XE0938)Last reviewed: February 2, 1998Article ID: Q111350 |
The information in this article applies to:
The "Introduction to Custom Dialog Boxes" (XE0938) Application Note shows you how to use the Dialog Editor to create a custom dialog box and provides sample macro code to display it. You can obtain this Application Note from the following sources:
THE TEXT OF XE0938
-------------------------------------------------------------------- ====================================================================== Microsoft(R) Product Support Services Application Note (Text File) XE0938: INTRODUCTION TO CUSTOM DIALOG BOXES ====================================================================== Revision Date: 2/94 No Disk Included The following information applies to Microsoft Excel, version 4.0. ----------------------------------------------------------------------- | INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY | | ACCOMPANY THIS DOCUMENT (collectively referred to as a | | Application Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | | EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | | IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A | | PARTICULAR PURPOSE. The user assumes the entire risk as to the | | accuracy and the use of this Application Note. This Application | | Note may be copied and distributed subject to the following | | conditions: 1) All text must be copied without modification and | | all pages must be included; 2) If software is included, all files | | on the disk(s) must be copied without modification; 3) All | | components of this Application Note must be distributed together; | | and 4) This Application Note may not be distributed for profit. | | | | Copyright © 1993-1994 Microsoft Corporation. All Rights Reserved. | | Microsoft and MS-DOS are registered trademarks and Windows is a | | trademark of Microsoft Corporation. | | Macintosh is a registered trademark of Apple Computer, Inc. | ----------------------------------------------------------------------- OVERVIEWThis Application Note shows you how to use the Dialog Editor to create a custom dialog box and provides sample macro code to display it.
INTRODUCTIONIn order to create a custom dialog box, you need to create a dialog box definition table. To use the custom dialog box, you must write a macro or series of macros that will set the initial values (this is called "initializing" the dialog box), display the dialog box, and return entered information.
CREATING THE DIALOG BOX DEFINITION TABLEAlthough it is possible to create a dialog box directly on a macro sheet, it is easier to visualize the dialog box when you use the Dialog Editor. The Dialog Editor allows you to move, size, and delete dialog box items. The Dialog Editor also creates a dialog box definition table: a table that contains information about the items in your dialog box.
To start the Dialog Editor
<graphic deleted> Figure 1 - Default dialog box in the Dialog Editor
To add an item to a dialog box
The first item you add will be placed in the top left corner of the Dialog Editor. Subsequent additions are placed below the most recently entered item.
To move and size an itemTo move an item, use the mouse to select it and drag the item to a different area of the dialog box. -or- Double-click an item to bring up the Info dialog box. You can move and size items in the Info Dialog Box by changing the values in the X, Y, Width, and Height boxes. For example, in the Info box below you could change the position of the OK button by changing the values in the X and Y boxes.
NOTE: You must clear the Auto check box before you can move or size some items. For example, in the Default OK Info box below, you would have to clear the Auto check box by Height in order to change the height of the OK button.<graphic deleted> Figure 2 - Info dialog box for OK button For more information about working with items in a dialog box, see pages 264-266 of the "User's Guide 2."
CREATING A REGISTRATION FORM DIALOG BOXThe following procedures help you create a dialog box similar to the one in Figure 3. To create this dialog box, you must specify the dialog box items in the Dialog Editor and copy the dialog box definition table to a macro sheet. Before you run the dialog box, you may want to create a macro to set the values that will be displayed when you initially run the dialog box. Finally, you need to create a macro to display the dialog box. <graphic deleted> Figure 3 - Registration Form dialog box
CREATING THE DIALOG BOX DEFINITION TABLE FOR THE REGISTRATION FORM
To add the Name text item and text edit box
To increase the size of the dialog boxTo increase the size of the dialog box, position the insertion point along any edge of the box until it becomes a two-headed arrow. Drag the arrow to size the window.
To add the Classification text item and standard list box
To create the Class Choices group and option buttonsThe class choices options are located in a group box that contains option buttons. Grouping items is useful when you have items that are related. In this example, the classes, which are related, are placed in a group box: this means the user can select only one class at a time. Grouped items are moved and sized together. To add a group of five option buttons and text items for Calculus, Scuba Diving, Bus. Communications, Systems Development, and Corporate Finance, do the following:
To add the OK and Cancel buttons
COPYING THE DIALOG BOX DEFINITION TABLE TO A MACRO SHEETOnce you have created the dialog box in the Dialog Editor, you need to copy it to a macro sheet.
To copy the dialog box from the Dialog Editor to a macro sheet
| C | D | E | F | G | H | I 1 | Item | X | Y | Width | Height | Text |Init/Result -------------------------------------------------------------------- 2 | 538 194 Registration Form 3 | 5 11 17 Name: 4 | 6 73 16 160 5 | 5 13 52 Classification: 6 | 15 73 72 160 84 7 | 1 278 151 88 OK 8 | 2 406 150 88 Cancel 9 | 14 279 17 238 111 Class Choices 10 | 11 11 | 12 Calculus 12 | 12 Scuba Diving 13 | 12 Bus. Communications 14 | 12 Systems Development 15 | 12 Corporate Finance ----------------------------------------------------------------------Figure 4 - The dialog box definition table for the Registration Form dialog box The dialog box definition table contains seven columns and as many rows as there are items in your dialog box plus one additional row. This extra row defines the height and width of the overall box. The range of the definition table in Figure 4 is C2:I15. The first column of the definition table lists the item numbers of the items in your dialog box. Columns 2-5 give the dimensions of the items, and column 6 is the text displayed in the items. Column 7, the Init/Result column, is where information about what you enter and select in the dialog box is returned. If you enter the column headings in C1:I1, you can easily identify each column. For more information about dialog box items and their item numbers, see pages 274-279 in "User's Guide 2."
INITIALIZING THE DIALOG BOX
To create a title barYou can make your dialog box look more like the built-in Microsoft Excel dialog boxes by creating a title bar. To create a title bar, enter the title in the text column of the first row of the definition table (cell H2 in this example). The title of the dialog box in Figure 4 is Registration Form. Creating a title bar allows you to move your dialog box around on the screen. Without the title bar, the box is unmovable or "static." For more information about changing an existing dialog box, see page 269 in "User's Guide 2."
To initialize the dialog boxYou can set the initial values in the Init/Result column of the dialog box definition table with the SET.VALUE() macro command. You may want to set items that return information to the Init/Result column to null before you run the dialog box. For example, you may want to clear this column for items that have more than one option to select (such as a multiple-selection list box) or one-option items (such as the Name edit box). To clear items that have more than one option to select, use the #N/A value; to clear items that have only one option, use two quotation marks (""). The sample macro code in Figure 5 sets the edit box, list box, and group boxes to null.
| A | B | Registration_Dialog_Box | Comments ------------------------------------------------------------- 1 | =SET.VALUE(I4,"") Sets edit box to null 2 | =SET.VALUE(I6,#N/A) Sets list box to null 3 | =SET.VALUE(I10,#N/A) Sets option group to null -------------------------------------------------------------Figure 5 - Sample macro code to set initial values IMPORTANT: When you enter macro code in columns A and B, be careful when you insert entire rows or shift columns or cells. Inserting an entire row or shifting columns or cells may change the dialog box definition table (which starts in cell C2 in this example) and may cause your dialog box macro to fail.
To add items to a list boxTo add items to a list box, first enter the items in a cell on a worksheet or macro sheet. To make the text appear in the box, reference the list in the Text column for the appropriate list box. This reference can be in R1C1 reference style, a defined name, or a defined array. In this example, the list items are in cells K2 to K5 on the macro sheet and are defined as <list>.
| C | D | E | F | G | H | I |J| K 1 | Item | X | Y | Width | Height | Text |Init/Result| | List ---------------------------------------------------------------------- 2 | 538 194 Registration Form Freshman 3 | 5 11 17 Name: Sophomore 4 | 6 73 16 160 Junior 5 | 5 13 52 Classification: Senior 6 | 15 73 72 160 84 R2C11:R5C11 2 7 | 1 278 151 88 OK 8 | 2 406 150 88 Cancel 9 | 14 279 17 238 111 Class Choices 10 | 11 4 11 | 12 Calculus 12 | 12 Scuba Diving 13 | 12 Bus. Communications 14 | 12 Systems Development 15 | 12 Corporate Finance ------------------------------------------------------------------------Figure 6 - The updated dialog box definition table
CREATING MACROS THAT DISPLAY YOUR DIALOG BOX AND RETURN ENTERED VALUES
To display your dialog boxTo view the dialog box, you need to create a macro that uses the DIALOG.BOX() macro command. The syntax of DIALOG.BOX() is DIALOG.BOX (<dialog_ref>), where <dialog_ref> is a reference to the dialog box definition table. This reference can be a defined name or a cell range. If you use the name that you define immediately after pasting your dialog box into the macro sheet from the Dialog Editor, you can eliminate the possibility of leaving out a row or column. To display the Registration Form dialog box, enter the following macro code starting in cell A1:
| A | B 1 | Registration_Dialog_Box | Comments ---------------------------------------------------------- 2 | =SET.VALUE(I4,"") Sets edit box to null 3 | =SET.VALUE(I6,#N/A) Sets list box to null 4 | =SET.VALUE(I10,#N/A) Sets option group to null 5 | =DIALOG.BOX(RegBox) Displays dialog box 6 | =RETURN() Ends macro ----------------------------------------------------------To run the macro:
table until the Cancel button is chosenWhen you use the macro you created in the procedure on page 5 to run your dialog box, every time you want to add a new name in the Name box you have to rerun the macro. To redisplay the dialog box after each entry until the user chooses the Cancel button, put the code that displays the dialog box in a loop, such as a WHILE-NEXT loop. The code in Figure 7 is an example of the macro with this added code.
| A | B 1 | Registration_Dialog_Box | Comments ---------------------------------------------------------- 2 | =SET.VALUE(I4,"") Sets edit box to null 3 | =SET.VALUE(I6,#N/A) Sets list box to null 4 | =SET.VALUE(I10,#N/A) Sets option group to null 5 | =SET.NAME("result", TRUE) Defines variable <result> | and sets it to TRUE 6 | =WHILE(result<>FALSE) Tests value of <result> 7 | result=DIALOG.BOX(regbox) Displays dialog box 8 | =SET.VALUE(I4,"") Sets edit box to null 9 | =SET.VALUE(I6,#N/A) Sets list box to null 10 | =SET.VALUE(I10,#N/A) Sets option group to null 11 | =NEXT() Ends WHILE-NEXT loop 12 | =RETURN() Ends macro ----------------------------------------------------------Figure 7 - Macro code to display dialog box until Cancel button is chosen When result is set to TRUE in A5, the dialog box is displayed at least once. If the user chooses OK, <result> is a number. When the user chooses Cancel, <result> is FALSE. The WHILE statement in A6 checks the value of result to make sure it is not equal to FALSE. If the value of <result> is not FALSE, the dialog box is displayed again, with all items set to null. The items are reset to null with the statements in cells A8:A10 (see A8:A10 in Figure 7).
Using INDEX() to Return List SelectionsWhen you select options from a list box or group box, numbers are returned to the Init/Result Column. These numbers represent the position of the selected item. In this example, when Sophomore is selected for Class selection, 2 is returned to cell I6 because the Sophomore Classification is the second item in the list. When Systems Development is selected, the number 4 is returned to cell I11 because Systems Development is the fourth option in the group.
C D E F G H I |J| K 1 | Item | X | Y | Width | Height | Text |Init/Result| | List ---------------------------------------------------------------------- 2 | 538 194 Registration Form Freshman 3 | 5 11 17 Name: Sophomore 4 | 6 73 16 160 Junior 5 | 5 13 52 Classification: Senior 6 | 15 73 72 160 84 R2C11:R5C11 2 7 | 1 278 151 88 OK 8 | 2 406 150 88 Cancel 9 | 14 279 17 238 111 Class Choices 10 | 11 4 11 | 12 Calculus 12 | 12 Scuba Diving 13 | 12 Bus. Communications 14 | 12 Systems Development 15 | 12 Corporate Finance -----------------------------------------------------------------------Figure 8 - Dialog box definition table To return the actual text of the choice, use the INDEX() function. The syntax of INDEX() is INDEX(<reference>, <rownum>), where the <reference> argument is the range containing the complete list, and rownum is a reference to the cell in the Init/Result column containing the value returned to the definition table. For this example, you need two INDEX() statements because two items are selected. The following is the macro with the added code:
A B 1 | Registration_Dialog_Box | Comments ------------------------------------------------------------------- 2 | =SET.VALUE(I4,"") Sets edit box to null 3 | =SET.VALUE(I6,#N/A) Sets list box to null 4 | =SET.VALUE(I10,#N/A) Sets option group to null 5 | =SET.NAME("result", TRUE) Defines variables <result> and | sets it to TRUE 6 | =WHILE(result<>FALSE) Tests value of <result> 7 | result=DIALOG.BOX(REGBOX) Displays dialog box 8 | =INDEX(H11:H15,I10) Indexes the items in the group box 9 | =INDEX(K2:K4,I6) Indexes the items in the list box 10 | =ALERT("Student "&I4&", Displays an alert box | whose classification is | "&A9&", is registered for | "&A8&".") 11 | =SET.VALUE(I4,"") Sets edit box to null 12 | =SET.VALUE(I6,#N/A) Sets list box to null 13 | =SET.VALUE(I10,#N/A) Sets option group to null 14 | result=DIALOG.BOX(REGBOX) Displays dialog box 15 | =NEXT() Ends WHILE-NEXT loop 16 | =RETURN() Ends macro -------------------------------------------------------------------Figure 9 - Macro code to display text of list selections
MORE INFORMATIONFor more information about creating and using dialog boxes in Microsoft Excel, see the Application Note "Using Dynamic Dialog Boxes" (XE0449).
TO OBTAIN THIS APPLICATION NOTE
|
Additional query words: 4.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |