How to Move Controls Between Forms in VB for Windows

Last reviewed: June 21, 1995
Article ID: Q79884
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

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.

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

  2. From the File menu, choose New Form (press ALT, F, F). Form2 will be created.

  3. From the File menu, choose New Module (press ALT, F, M). Module1 will be created.

  4. Create the following controls for both Form1 and Form2:

        Control          Name         Property Setting
        -------          ----------   ----------------
        Command button   Command1()   Index = 0
        Command button   Command2     Caption = "Enable Drag"
    
       (In Visual Basic version 1.0 for Windows, set the CtlName Property
        for the above objects instead of the Name property.)
    
    

  5. 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 "USER" () As Integer
    Declare Function GetParent Lib "USER" (ByVal hWnd As Integer) As Integer
    
    

  6. Add the following code to the General Declarations section of Form1:

    Dim EnableDrag As Integer

  7. 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 = "Button" + Str$(i%)
          Command1(i%).Visible = -1
       Next i%
    End Sub
    
    

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

  9. 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 = "Disable Drag"
       Else
          EnableDrag = 0
          Command2.Caption = "Enable Drag"
       End If
    
       For i% = 0 To 4                        ' Set DragMode for Controls.
          Command1(i%).DragMode = EnableDrag
       Next i%
    End Sub
    
    

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

  11. Add the following code to the General Declarations section of Form2:

Dim EnableDrag As Integer

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

  2. Add the following code to the Command1_Click event procedure of

        Form2:
    

    Sub Command1_Click (Index As Integer)
       Button_Clicked Command1(Index)
    End Sub
    
    

  3. Add the following code to the Command2_Click event procedure of

        Form2:
    

    Sub Command2_Click ()
       If EnableDrag = 0 Then
          EnableDrag = 1
          Command2.Caption = "Disable Drag"
       Else
          EnableDrag = 0
          Command2.Caption = "Enable Drag"
       End If
    
       For i% = 0 To 4
          Command1(i%).DragMode = EnableDrag
       Next i%
    End Sub
    
    

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

  5. Add the following code to Module1:

    Sub Button_Clicked (Source As Control) ' Generic Click routine.
        MsgBox "Button" + Str$(Source.Index) + " Clicked!!!"
    End Sub
    
    

  6. From the Run menu, choose Start (press ALT, R, S) to run the

        program.
    

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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.