Absolute Changes

Using an absolute approach, code has to be written to set the size and position of all the controls on the form, relative to the form's new dimensions and to each other. Consider a very simple form showing just a ListBox and an OK button.

The code to resize and reposition the two controls using absolute methods is as follows:

Private Sub UserForm_Resize()

'Handle the form's resizing by specifying the new size and position 'of all the controls

'Use a standard gap of 6 points between controls Const dGap = 6

'Ignore errors caused by controls getting too small On Error Resume Next

'The OK button is in the middle ... btnOK.Left = (Me.InsideWidth - btnOK.Width) / 2

'... and at the bottom of the form, with a standard gap below it btnOK.Top = Me.InsideHeight - dGap - btnOK.Height

'The list's width is the form's width, 'minus two gaps for the left and right edges lstItems.Width = Me.InsideWidth - dGap * 2

'The list should fill the space between the top of the form 'and the top of the OK button, minus a gap top and bottom lstItems.Height = btnOK.Top - dGap * 2

End Sub

It works, but has a few major problems:

□ Specific code has to be written for every control that changes size or position, which can be a daunting task for more complex forms. See the resize code in FormFun.xlsm for an example of this.

□ The size and position of controls are often dependent on the size and position of other controls (such as the bottom of the ListBox being four pixels above the top of the OK button).

□ If you modify the appearance of the form by adding or moving controls, you have to make corresponding changes to the resize code. For example, to add a Cancel button alongside the OK button, you have to add code to handle the Cancel button's repositioning and also change the code for the OK button.

□ There is no opportunity for code reuse.

0 0

Post a comment