[ Team LiB ] |
Recipe 2.7 Size a Form's Controls to Match the Form's Size2.7.1 ProblemWindows users have become accustomed to resizing forms on their screens. A professional-looking application will proportionally resize the controls on a form when you stretch or shrink that form. You'd like to be able to resize your forms while the application is running and have the controls on the form react appropriately. For example, the Database Explorer window's list box expands when you expand the window. How can you do this on your own forms? 2.7.2 SolutionBecause Access can notify your application when the user resizes a form, you can attach code to the Resize form event and react to the change in size. Access also triggers this event when it first draws the form, so you can place your controls correctly then, too. Base your calculations on the form's InsideWidth and InsideHeight properties. Load and run the form frmExpando in 02-07.MDB. Resize the form and watch the size of the large text box. Also notice the positions of the two command buttons. Figure 2-13 shows the form in design mode, and Figure 2-14 shows the form sized to different proportions. Though it's perfectly reasonable to change the size of all the controls, this form does not. It uses three different techniques: Figure 2-13. frmExpando in design mode
Figure 2-14. frmExpando at runtime, with different proportionsThe code that does the work in this case is specific to the particular form. Follow the steps below to create a form similar to frmExpando. Once you've gone through these steps, you should be able to expand on the concepts (pun intended) and create your own self-sizing forms.
2.7.3 DiscussionThe code used in this solution reacts to the Resize events that occur when you resize a form in run mode (and when you open the form). The code retrieves the form's current size (its InsideWidth and InsideHeight properties) and resizes the controls accordingly. This example starts out by checking a flag, fInHere, and causes the subroutine to exit if the variable's value is True. It's possible that the procedure itself might cause another Resize event (if you've sized the form smaller than the preset minimum size); this flag ensures that the routine doesn't do more work than it needs to do.
The code next retrieves the current form size and stores the values into local variables. By placing these values into variables, Access eliminates the need to retrieve the values of the properties every time you need to use them. This speeds up the operation, because retrieving property values is expensive in terms of operating speed. ' Get the current screen coordinates. intHeight = Me.InsideHeight intWidth = Me.InsideWidth Once it has retrieved the sizes, the procedure verifies that the form hasn't been sized too small by the user. If it has been, it forces the form to be at least as large as the preset values of acbcMinWidth and acbcMinHeight: If intWidth < acbcMinWidth Then DoCmd.MoveSize , , acbcMinWidth intWidth = Me.InsideWidth End If If intHeight < acbcMinHeight Then DoCmd.MoveSize , , , acbcMinHeight intHeight = Me.InsideHeight End If Finally, the procedure sets the sizes and locations of each of the controls based on the new width and height of the form. First, it sets the height of the form's detail section, Section(0), so that there will be room for all of the controls at the new height. It then sets the width and height of the text box and sets the left coordinates of the command buttons. This preserves their sizes but resets their positions: Set ctl = Me.txtEntry With ctl .Height = intHeight - (.Left + .Top) .Width = intWidth - Me.cmdOK.Width - (3 * .Left) End With ' Set the positions of the two buttons. With Me.cmdOK .Left = intWidth - .Width - ctl.Left End With With Me.cmdCancel .Left = intWidth - .Width - ctl.Left End With The values used as offsets in this example were all arbitrarily chosen. They work for this particular example, but you'll need to vary them for your own forms. Remember, also, that this example was quite simple. You'll be doing many more calculations if you want to resize a multicolumn list box, for example. In any case, the concepts are the same: resize each of the controls based on the current size of the form. The tricky part is finding some "reference" on which you can base your sizing decisions; in this example, we used the offset of the expanding text box from the left edge of the form. |
[ Team LiB ] |