Relative Changes

Using a relative approach, information is added to each control to specify by how much that control's size and position should change as the UserForm's size changes. In the same dialog, the two controls have the following relative changes:

□ The OK button should move down by the full change in the form's height (to keep it at the bottom).

□ The OK button should move across by half the change in the form's width (to keep it in the middle).

□ The ListBox's height and width should change by the full change in the form's height and width.

These statements can be encoded into a single string to state the percentage change for each control's Top, Left, Height, and Width properties, and can be stored against the control. A convenient place to store it is the control's Tag property, which allows the resizing behavior of the control to be set at design time. Using the letters T, L, H, and W for the four properties, and a decimal for the percentage change if not 100%, gives the Tag properties of HW for the list box and TL0.5 for the OK button, as shown in Figure 27-2.

A Resizable Userform - Relative @

|Tag = HW |

OK |lag =TL0.5|

Figure 27-2

When the UserForm_Resize event fires, the code can calculate the change in the form's height and width and iterate through all the controls, adjusting their Top, Left, Height, and Width as specified by their Tag properties. The CFormResizer class to do this is shown in the next section.

There are a number of benefits to this approach:

□ The resize behavior of each control is set at design time, while the form is being viewed, just like all the other properties of the control.

□ The change in size and position of each control is independent of any other control.

□ Controls can be added, moved, or deleted without having to modify the Resize code or change other controls' resize behavior.

□ The resize code can treat every control in exactly the same way; hence, every UserForm uses exactly the same Resize code, which can be encapsulated in a separate class module.

0 0

Post a comment