How to Move Controls Between Forms in VB for WindowsID: Q79884
|
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.
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
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.)
' Windows API function declarations.
Declare Function GetFocus Lib "USER" () As Integer
Declare Function GetParent Lib "USER" (ByVal hWnd As Integer) As Integer
Dim EnableDrag As Integer
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
Sub Command1_Click (Index As Integer)
Button_Clicked Command1(Index) ' Call Routine in MODULE1.BAS.
End Sub
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
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
Dim EnableDrag As Integer
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
Sub Command1_Click (Index As Integer)
Button_Clicked Command1(Index)
End Sub
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
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
Sub Button_Clicked (Source As Control) ' Generic Click routine.
MsgBox "Button" + Str$(Source.Index) + " Clicked!!!"
End Sub
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 23, 1999