The information in this article applies to:
- Microsoft Visual Basic programming system for Windows,
versions 1.0, 2.0, and 3.0
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
- Start a new project in Visual Basic. Form1 is created by default.
- Add a Text box (Text1) and Command button (Command1) to the form.
- Place the following code in the (general) (declarations) section of
Form1:
Dim moving_flag% 'moving flag to toggle moving ability
- 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
- 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
- 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
- Place the following code in the Text1 Click event:
Sub Text1_Click ()
moving_flag% = 0
End Sub
- 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.
|