How to Reset the Parent of a Visual Basic Control

ID Number: Q80189

1.00

WINDOWS

Summary:

Visual Basic does not support overlapping controls. This can be a

problem if you want to drag and drop a control from one parent control

to another parent control. Using the Windows 3.0 API SetParent

function call, you can change a control's parent within Visual Basic.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

A frame, picture box, and form can act as parent controls. Creating a

control on top of any of these parent controls creates that control as

a child of the parent. When you use the Drag operations, there may be

times when you want to move a child control from one parent control to

another parent. If you allow the movement and don't change the child's

parent, you are creating overlapping controls, which are not supported

in Visual basic.

The SetParent function changes the parent of a child control.

SetParent has the following description:

SetParent%(ByVal hWndChild, ByVal hWndParen%)

---------------------------------------------

Parameter Type/Description

---------- ----------------

hWndChild HWnd/Identifies the child window

hWndParent HWnd/Identifies the parent window

The returned value identifies the previous parent window.

The example below demonstrates how to drag and drop a text box between

the form and a picture box on the form. The parent controls are the

picture box and the form. The child control is the text box.

To create this example, add the following controls:

Control CtlName

------- -------

Form Form1

Text box Text1

Picture box Picture1

Command button Command1

Add the code listed below to your program. The Command1 button will be

used to start the dragging operation.

1. Press the command button.

2. Place the cursor over the text box.

3. Press the left mouse button and drag the text box either over the

picture control or over the form.

4. Once the text box is over the control, release the mouse button.

For better control of where the text box is placed, turn off Grid

Setting from the Edit menu of Visual Basic.

'============= GLOBAL.BAS ==================

Declare Function SetParent% Lib "user" (ByVal h%, ByVal h%)

Declare Function GetFocus% Lib "user" ()

' GetFocus will be used to obtain the handles to the

' controls. This is not build into every control of Visual

' Basic

'============= FORM1 =======================

Dim hWndText As Integer

Dim hWndPicture As Integer

Sub Form_Load ()

'form has to be shown to access any of the controls

Show

'get the handle to the text box

Text1.SetFocus

hWndText = GetFocus()

'get the handle to the picture box

Picture1.SetFocus

hWndPicture = GetFocus()

End Sub

Sub Picture1_DragDrop (Source As Control, X As Single, Y As Single)

G% = SetParent(hWndText, hWndPicture)

Source.Move X - Source.Width / 2, Y - Source.Height / 2

Source.DragMode = 0

End Sub

Sub Form_DragDrop (Source As Control, X As Single, Y As Single)

G% = SetParent(hWndText, Form1.hwnd)

Source.Move X - Source.Width / 2, Y - Source.Height / 2

Source.DragMode = 0

End Sub

Sub Command1_Click ()

'start the dragging process

Text1.DragMode = 1

End Sub

Additional reference words: 1.00