Microsoft Office Tutorials and References
In Depth Information
A Resizable UserForm
Do a bit of creative thinking about these facts, and you see that it’s possible to translate the
user’s movement of a Label control into information that you can use to resize a UserForm.
When the user clicks the objResizer Label object, the objResizer_MouseDown
eventhandler procedure is executed:
Private Sub objResizer_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
LeftResizePos = X
TopResizePos = Y
End If
End Sub
This procedure executes only if the left mouse button is pressed (that is, the Button argument is
1) and the cursor is on the objResizer label. The X and Y mouse coordinates at the time of the
button click are stored in module-level variables: LeftResizePos and TopResizePos .
Subsequent mouse movements fire the MouseMove event, and the objResizer_MouseMove
event handler kicks into action. Here’s an initial take on this procedure:
Private Sub objResizer_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
With objResizer
.Move .Left + X - LeftResizePos, .Top + Y - TopResizePos
Me.Width = Me.Width + X - LeftResizePos
Me.Height = Me.Height + Y - TopResizePos
.Left = Me.InsideWidth - .Width
.Top = Me.InsideHeight - .Height
End With
End If
End Sub
If you study the code, you’ll see that the UserForm’s Width and Height properties are adjusted,
based on the movement of the objResizer Label control. Figure 15-20 shows how the
UserForm looks after the user moves the Label control down and to the right.
The problem, of course, is that the other controls in the UserForm don’t respond to the
UserForm’s new size. The ListBox should be expanded, and the CommandButton should be
relocated so that it remains in the lower-left corner.
 
Search JabSto ::




Custom Search