Microsoft KB Archive/113904

= 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.



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 </li> 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.</li></ol>

Additional query words: 2.00 3.00

Keywords: KB113904

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.