[ Team LiB ] |
Recipe 9.8 Create an Expanding Dialog9.8.1 ProblemYou have a dialog with a lot of options, most of which are needed only in specific situations. You'd like to create this form as an expanding dialog, similar to forms that have an Advanced button revealing more options. How can you do this with your own form? 9.8.2 TechniqueYou can make a hidden section of the form become visible at runtime, and use the Window | Size to Fit Form command to force the form to expand to fit its new dimensions. This solution shows you how to create this type of form using an expanding form footer. You'll also learn how to minimize screen flashing while resizing the form by manipulating the form's Painting property. Follow these steps to create your own expanding dialog form:
Figure 9-24. The frmExpandingDialog form in design viewTo demonstrate this new functionality, load the sample database 09-08.MDB and open frmExpandingDialog in form view. The dialog form will display in its initial, contracted state (see Figure 9-25). Click on the Advanced button and the form will expand to reveal additional text boxes (see Figure 9-26). Click again on the button (now labeled Basic) to return to the contracted state. (This sample form is for demonstration purposes only; it doesn't do anything with the data you enter into it.) Figure 9-25. The frmExpandingDialog form in its contracted stateFigure 9-26. The frmExpandingDialog form in its expanded state9.8.3 DiscussionBecause you set the Visible property of the form footer section to No, the footer does not appear when the form is first opened. In addition, because you set the AutoResize property to Yes, Access resizes the form to show only the visible areas of the form. Expansion of the form is handled by the code attached to the cmdExpand button's Click event. This event procedure begins by defining a few constants and variables. The two constants will be used later in the function to shift the focus to the first control of each section: Dim sct As Section Dim blnExpanded As Boolean Const acbFirstBasicCtl = "txtFirstName" Const acbFirstAdvancedCtl = "txtOldPW" Next, the procedure sets the section variable to point to the form's footer section, using the built-in acFooter constant. In addition, it stores the current state of the Visible property of the section in the Boolean variable blnExpanded: Set sct = Me.Section(acFooter) ' Keep track of the state of the form when first called. blnExpanded = sct.Visible If the form is currently contracted, it needs to be expanded, and vice versa. But before this is done, the code sets the form's Painting property to False if (and only if) the form is being expanded. The technique will work without performing this step, but the form will flash as it expands. On the other hand, if the form is being contracted, you shouldn't turn off Painting; if you do, the form will not properly repaint itself and the nonfunctional advanced section will remain painted on the screen. This step is accomplished with a single line of code and six lines of comments: ' If the form is in nonexpanded state, turn off ' form painting while expanding the form. This ' prevents the form from flashing. ' If the form is in expanded state, however, Access ' won't hide the expanded portion unless form ' painting is left on. If Not blnExpanded Then Me.Painting = False The form is then expanded or contracted by using Not to toggle the footer section's Visible property to the opposite of its current state: ' Expand form if currently unexpanded and vice versa. sct.Visible = Not blnExpanded The code then resizes the form using the RunCommand method of the DoCmd object to carry out the Window Size to Fit Form menu command: ' Size to fit the form to expand or contract the form's ' borders to match the visibility of the section. DoCmd.RunCommand acCmdSizeToFitForm The function then changes the caption of the button, turns painting back on if it was turned off, and finally moves the focus to the first control of the appropriate section. This last step is not absolutely necessary, but it's a nice touch because the normal tab sequence will not jump across sections. The relevant code is: ' Change the button caption and repaint if necessary. If Not blnExpanded Then Me.cmdExpand.Caption = "Basic <<" Me.Painting = True Me(acbcFirstAdvancedCtl).SetFocus Else Me.cmdExpand.Caption = "Advanced >>" Me(acbcFirstBasicCtl).SetFocus End If You can also apply this technique to non-dialog and bound forms. Although it's not commonly done, there's nothing to stop you from placing bound controls in the footer section of a form. On the other hand, it may be more appropriate to use a tabbed form for bound forms. See the Solution in Recipe 2.5 for more details. |
[ Team LiB ] |