|Let the user move and resize controls at run time in Visual Basic 6
|This example shows how to let the user move and resize controls at run time in Visual Basic 6.
|drag, move, resize, controls, Visual Basic 6
This example is from my book Custom Control Library. This book includes 101 custom ActiveX controls written for Visual Basic 5 and 6.
This example builds an ActiveX control that allows the user to move and resize the control at run time. It automatically resizes the control it contains to fit.
Note that I don't necessarily recommend this strategy. Users will eventually do something silly like give a control zero size or move it off the form so be sure you have a way to reset the controls if necessary.
The UserControl contains a small PictureBox named Corner in its lower right corner. That control tracks its MouseDown, MouseMove, and MouseUp events. As you drag that control, the following code resizes the UserControl, puts Corner back in the lower right corner, and resizes contained controls to fit.
Private Sub UserControl_Resize()
Dim hgt As Single
Dim ctl As Control
' Do nothing at design time.
If Not Ambient.UserMode Then Exit Sub
' Position the corner dragger.
hgt = ScaleHeight - m_HandleSize
If hgt < 1 Then hgt = 1
Corner.Move ScaleWidth - m_HandleSize, _
hgt, m_HandleSize, m_HandleSize
' Position the contained controls.
For Each ctl In ContainedControls
ctl.Move 0, 0, ScaleWidth, hgt
The UserControl arranges the controls it contains so there is a small gap at the bottom. That lets the UserControl show through and lets you see the Corner PictureBox.
The UserControl tracks MouseDown, MouseMove, and MouseUp events on its own surface, too. When you click and drag on the UserControl, the code figures out where the control should be and calls Extender.Move to move the UserControl appropriately.
The control also includes routines to save and restore size and position in the Registry so the program can easily keep its size and position between program runs.
See the code for additional details.