PDA

View Full Version : Drag the Excel userform



ahler
03-21-2009, 09:36 AM
Hi, I'm new to VAB. Can anyone tell me how to drag the userform into smaller size instead of add in a toggle button to minimize it? Thanks in advance.

mikerickson
03-28-2009, 09:38 PM
At design time, the size of the UF is controled by the Width and Height properties in the Properties Window. The handle on the VB Editor is the editor's window only. I know of no way to freehand adjust the size of the UF at design time.

If you want the user to be able to resize a userform at run time, a Label can give the user a control in the lower right corner to grab and resize the userform.

Code changing the size and location of other controls will need to be added.

Dim xOffset As Single
Dim yOffset As Single

Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Rem me.width = (lable1.left + x) + xOffset
Rem thus, xOffset = me.width - (label1.left + x)
If Button = 1 Then
If x > 0 Then xOffset = Me.Width - (Label1.Left + x)
If y > 0 Then yOffset = Me.Height - (Label1.Top + y)
End If
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
If (xOffset * yOffset <> 0) And (Button = 1) Then
Me.Width = (Label1.Left + x) + xOffset
Label1.Left = Me.Width - Label1.Width
Me.Height = (Label1.Top + y) + yOffset
Label1.Top = Me.Height - 22 - Label1.Height
End If
End Sub

Private Sub Label1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
xOffset = 0
yOffset = 0
End Sub

Private Sub UserForm_Initialize()
With Label1
.Width = 20
.Left = Me.Width - .Width
.Height = 20
.Top = Me.Height - 22 - .Height: Rem 22 is height of caption bar
End With
End Sub
PLEASE TRY THE ROUTINE BEFORE READING THE FOLLOWING:


there is a quirk, the first time the user clicks on the label, nothing happens, its initializing,
i wonder if an unwarned user will notice it.

shades
04-06-2009, 07:19 AM
Howdy. Is this specific to the Mac?

mikerickson
04-06-2009, 05:42 PM
Untested, but I think it would work on a Windows machine.

The "trick" would be to define every control's position relative to the userform's width and height.
Note that the code in the MouseMove event setting the .Left and .Top of Label1 is the same as the code in the Initialize event.

With more controls than just the label, a sub PlaceAllControls would be useful, to be called in the Initialize event and in the MouseMove.