Microsoft KB Archive/79884

= How to Move Controls Between Forms in VB for Windows =

Article ID: 79884

Article Last Modified on 12/12/2003

-

APPLIES TO


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

-



This article was previously published under Q79884



SUMMARY
Microsoft Visual Basic for Windows does not support the actual movement of controls between forms. Attempting to change the parent/child relationship of a control from one form to another can result in unpredictable behavior.

However, by creating a control array of the same control type on each form, and by creating a subroutine or function in a Visual Basic for Windows module, you can simulate the movement of a control from one form to another. An example of how to do this is listed below.



MORE INFORMATION
This example uses the Windows API functions GetFocus and GetParent to determine the origin of the control dropped onto a form. For more information on GetFocus and GetParent, query separately on the following words in the Microsoft Knowledge Base:

GetFocus GetParent

The following steps demonstrate how to simulate the movement of controls between two forms. Note that you can improve this example by Loading and Unloading the controls as they are needed.

 Start Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 will be created by default. From the File menu, choose New Form (press ALT, F, F). Form2 will be created. From the File menu, choose New Module (press ALT, F, M). Module1 will be created.  Create the following controls for both Form1 and Form2:    Control          Name         Property Setting ---         --       Command button   Command1   Index = 0 Command button  Command2     Caption = &quot;Enable Drag&quot; (In Visual Basic version 1.0 for Windows, set the CtlName Property   for the above objects instead of the Name property.)

  Add the following code to the Module1 (or GLOBAL.BAS in Visual Basic version 1.0 for Windows): ' Windows API function declarations. Declare Function GetFocus Lib &quot;USER&quot; As Integer Declare Function GetParent Lib &quot;USER&quot; (ByVal hWnd As Integer) As Integer   Add the following code to the General Declarations section of Form1: Dim EnableDrag As Integer

</li>  Add the following code to the Form_Load event procedure of Form1: Sub Form_Load ' Move the form to the left half of the screen. Move 0, Top, Screen.Width \ 2 Form2.Show EnableDrag = 0 Command1(0).Top = 0 Command1(0).Left = 100

For i% = 1 To 4                            ' Load Control Array. Load Command1(i%) Command1(i%).Left = Command1(i% - 1).Left Command1(i%).Top = Command1(i% - 1).Top + Command1(i% - 1).Height Next i%

For i% = 0 To 4                       ' Define Control Properties. Command1(i%).Caption = &quot;Button&quot; + Str$(i%) Command1(i%).Visible = -1 Next i% End Sub </li>  Add the following code to the Command1_Click event procedure of Form1: Sub Command1_Click (Index As Integer) Button_Clicked Command1(Index)       ' Call Routine in MODULE1.BAS. End Sub </li>  Add the following code to the Command2_Click event procedure of Form1: Sub Command2_Click If EnableDrag = 0 Then                     ' Toggle DragMode. EnableDrag = 1 Command2.Caption = &quot;Disable Drag&quot; Else EnableDrag = 0 Command2.Caption = &quot;Enable Drag&quot; End If

For i% = 0 To 4                       ' Set DragMode for Controls. Command1(i%).DragMode = EnableDrag Next i% End Sub </li>  Add the following code to the Form_DragDrop event procedure of Form1: Sub Form_DragDrop (Source As Control, X As Single, Y As Single) Source.SetFocus                    ' Get Parent of Source Control. CtrlHnd% = GetFocus Parent% = GetParent(CtrlHnd%)

If Parent% <> Form1.hWnd Then      ' If Parent is other Form. Index% = Source.Index Command1(Index%).Caption = Source.Caption Command1(Index%).Left = Source.Left Command1(Index%).Top = Source.Top Command1(Index%).Width = Source.Width Command1(Index%).Height = Source.Height Command1(Index%).Visible = -1 Source.Visible = 0 End If End Sub </li>  Add the following code to the General Declarations section of Form2: Dim EnableDrag As Integer </li>  Add the following code to the Form_Load event procedure of Form2: Sub Form_Load ' Move the form to the right half of the screen. Move Screen.Width \ 2, Top, Screen.Width \ 2 EnableDrag = 0 Command1(0).Visible = 0 For i% = 1 To 4                       ' Load Control Array. Load Command1(i%) Command1(i%).Top = Command1(i% - 1).Top + Command1(i% - 1).Height Command1(i%).Visible = 0 Next i% End Sub </li>  Add the following code to the Command1_Click event procedure of Form2: Sub Command1_Click (Index As Integer) Button_Clicked Command1(Index) End Sub </li>  Add the following code to the Command2_Click event procedure of Form2: Sub Command2_Click If EnableDrag = 0 Then EnableDrag = 1 Command2.Caption = &quot;Disable Drag&quot; Else EnableDrag = 0 Command2.Caption = &quot;Enable Drag&quot; End If

For i% = 0 To 4 Command1(i%).DragMode = EnableDrag Next i% End Sub </li>  Add the following code to the Form_DragDrop event procedure of Form2: Sub Form_DragDrop (Source As Control, X As Single, Y As Single) Source.SetFocus          ' Determine Parent of Source. CtrlHnd% = GetFocus Parent% = GetParent(CtrlHnd%) If Parent% <> Form2.hWnd Then Index% = Source.Index Command1(Index%).Caption = Source.Caption Command1(Index%).Left = Source.Left Command1(Index%).Top = Source.Top Command1(Index%).Width = Source.Width Command1(Index%).Height = Source.Height Command1(Index%).Visible = -1 Source.Visible = 0 End If End Sub </li>  Add the following code to Module1: Sub Button_Clicked (Source As Control) ' Generic Click routine. MsgBox &quot;Button&quot; + Str$(Source.Index) + &quot; Clicked!!!&quot; End Sub </li> From the Run menu, choose Start (press ALT, R, S) to run the program.</li></ol>

To drag controls from one form to the other, choose the Enable Drag button. Once this button has been activated on a form, you can drag any of the command buttons from one form to the other. The drag mode can be disabled by choosing the Disable Drag button. When drag mode has been disabled, clicking any of the command buttons on the form will cause a message box to be displayed.

Additional query words: 2.00 3.00

Keywords: KB79884

-

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

© Microsoft Corporation. All rights reserved.