Microsoft KB Archive/80189

= How to Reset the Parent of a Visual Basic Control =

Article ID: 80189

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 Q80189



SUMMARY
Visual Basic version 1.0 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 API SetParent function call, you can change a control's parent within Visual Basic.

Visual Basic versions 2.0 and 3.0 support overlapping controls with the z-order method. For more information on the z-order method, search for the z-order topic in the Visual Basic Help menu.



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.

Step-by-Step Example
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.

 Start Visual Basic or from the File menu, choose New Project if Visual Basic is already running. Form1 is created by default. Add a Text box (Text1) to Form1. Add a Picture box (Picture1) to Form1. Add a Command button (Command1) to Form1.  Add the following code to the Global module: '============= GLOBAL.BAS ================== Declare Function SetParent% Lib &quot;user&quot; (ByVal h%, ByVal h%) Declare Function GetFocus% Lib &quot;user&quot; ' GetFocus will be used to obtain the handles to the ' controls. This is not build into every control of Visual Basic   Add the following code to the general declarations section of Form1: '============= FORM1 ======================= Dim hWndText As Integer Dim hWndPicture As Integer </li>  Add the following code to the Form_Load event procedure of Form1: 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 </li>  Add the following code to the appropriate event procures: 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 </li> Run the program. The Command1 button is used to start the dragging operation.</li></ol>

Demonstration Steps
Try the following steps when running the application:


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

Additional query words: 2.00 3.00

Keywords: KB80189

-

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

© Microsoft Corporation. All rights reserved.