Knowledge Base |

# How to Move a Control Across a Form at Run Time

Article ID: 113904

Article Last Modified on 11/18/2003

APPLIES TO

- Microsoft Visual Basic 1.0 Standard Edition
- Microsoft Visual Basic 2.0 Standard Edition
- Microsoft Visual Basic 3.0 Professional Edition

This article was previously published under Q113904

## SUMMARY

This article shows by example how to use the Move method to move a text box across a form at run time. This is an alternative to the Drag and Drop method. You can alter the example code to move other controls at run time as long as the control has both a Top and Left property to set.

## MORE INFORMATION

### Example Showing How to Move Text Box Across Form at Run Time

1. Start a new project in Visual Basic. Form1 is created by default.
2. Add a Text box (Text1) and Command button (Command1) to the form.
3. Place the following code in the (general) (declarations) section of Form1:

          Dim moving_flag%   'moving flag to toggle moving ability

4. Place the following code in the Command1 Click event procedure of Form1:

          Sub Command1_Click ()
             moving_flag% = 1   'start the text box moving with the mouse
          End Sub

5. Place the following code in the Form Load event procedure of Form1:

          Sub Form_Load ()
             moving_flag% = 0   ' Initially set the flag to turn off the moving.
             Command1.Caption = "Turn moving on"
          End Sub

6. Place the following code in the Form MouseMove event procedure of Form1:

          ' Enter the following two lines as one, single line:
          Sub Form_MouseMove (Button As Integer, Shift As Integer, x As Single,
             y As Single)
             If moving_flag% = 1 Then   ' Condition to call the moving procedure.
                Text1.Move x, y
             End If
          End Sub

7. Place the following code in the Text1 Click event:

          Sub Text1_Click ()
             moving_flag% = 0
          End Sub

8. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button. Then click within the Text1 box to stop the moving.

Additional query words: 2.00 3.00

Keywords: KB113904