Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user drag controls to new positions and save the positions between runs
Keywordsdrag control, move control
CategoriesControls
 
If Shift is pressed, the controls' MouseDown event handlers call subroutine DragMouseDown to begin the drag. The controls' MouseMove and MouseUp event handlers calls subroutines DragMouseMove and DragMouseUp to continue and finish the drags.

The following code shows the event handlers for the Text2 control. The code for the other controls is similar.

 
Private Sub Text2_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Shift Then DragMouseDown Text2, X, Y
End Sub

Private Sub Text2_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DragMouseMove Text2, X, Y
End Sub

Private Sub Text2_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    DragMouseUp Text2, X, Y
End Sub
 
Subroutine DragMouseDown starts a drag.

Subroutine DragMouseMove calculates the control's new position and moves it there, keeping it on the form.

Subroutine DragMouseUp ends the drag.

 
' Start dragging.
Private Sub DragMouseDown(ByVal ctl As Control, ByVal X As _
    Single, ByVal Y As Single)
    m_Dragging = True
    m_StartX = X
    m_StartY = Y
End Sub

' Continue dragging.
Private Sub DragMouseMove(ByVal ctl As Control, ByVal X As _
    Single, ByVal Y As Single)
Dim new_x As Single
Dim new_y As Single

    If Not m_Dragging Then Exit Sub

    new_x = ctl.Left + (X - m_StartX)
    If new_x < 0 Then
        new_x = 0
    ElseIf new_x > ScaleWidth - ctl.Width Then
        new_x = ScaleWidth - ctl.Width
    End If

    new_y = ctl.Top + (Y - m_StartY)
    If new_y < 0 Then
        new_y = 0
    ElseIf new_y > ScaleHeight - ctl.Height Then
        new_y = ScaleHeight - ctl.Height
    End If

    ctl.Move new_x, new_y
End Sub

' End dragging.
Private Sub DragMouseUp(ByVal ctl As Control, ByVal X As _
    Single, ByVal Y As Single)
    If m_Dragging Then m_Dragging = False
End Sub
 
The program's Form_Load event handler uses GetSetting to restore the controls' previously saved positions.

The Form_Unload event handler uses SaveSetting to save the controls' positions.

Both of these routines use the ControlName function to get the controls' names including their indexes if they are in control arrays. Note that Form_Load and Form_Unload don't work with controls such as Line that do not have Left and Top properties or Move methods.

 
' Load saved control positions.
Private Sub Form_Load()
Dim ctl As Control
Dim ctl_name As String

    For Each ctl In Controls
        ' Get the control's name.
        ctl_name = ControlName(ctl)

        ' Load the control's saved position.
        On Error Resume Next
        ctl.Move _
            GetSetting("howto_let_user_move_controls", _
                "Positions", ctl_name & ".Left", ctl.Left), _
                _
            GetSetting("howto_let_user_move_controls", _
                "Positions", ctl_name & ".Top", ctl.Top)
    Next ctl
End Sub

' Save control positions.
Private Sub Form_Unload(Cancel As Integer)
Dim ctl As Control
Dim ctl_name As String

    For Each ctl In Controls
        ' Get the control's name.
        ctl_name = ControlName(ctl)

        ' Save the control's position.
        On Error Resume Next
        SaveSetting "howto_let_user_move_controls", _
            "Positions", ctl_name & ".Left", ctl.Left
        SaveSetting "howto_let_user_move_controls", _
            "Positions", ctl_name & ".Top", ctl.Top
    Next ctl
End Sub

' Return the control's name including its index
' if it is part of a control array.
Private Function ControlName(ByVal ctl As Control) As String
    On Error Resume Next
    ControlName = ctl.Name & "(" & ctl.Index & ")"
    If Err.Number <> 0 Then ControlName = ctl.Name
End Function
 
Note that the controls' positions are not saved if the program crashes and Form_Unload doesn't execute. A more paranoid application could save the controls' positions every time the user changed them.

Note also that it's not necessarily a good thing to let users move controls around. If you have a lot of users, some will create very confusing arrangements and need help. This program also doesn't try to change the controls' tab order, although you could do that.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated