How to Reset the Parent of a Visual Basic Control

Last reviewed: June 21, 1995
Article ID: Q80189
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

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.

  1. Start Visual Basic or from the File menu, choose New Project if Visual Basic is already running. Form1 is created by default.

  2. Add a Text box (Text1) to Form1.

  3. Add a Picture box (Picture1) to Form1.

  4. Add a Command button (Command1) to Form1.

  5. Add the following code to the Global module:

       '============= 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
    
    

  6. Add the following code to the general declarations section of Form1:

       '============= FORM1 =======================
       Dim hWndText As Integer
       Dim hWndPicture As Integer
    
    

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

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

  9. Run the program. The Command1 button is used to start the dragging operation.

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


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.