Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleReparent controls from one form to another
DescriptionThis example shows how to reparent controls from one form to another in Visual Basic 6.
Keywordsreparent, reparent control
CategoriesControls, API
 
Use the SetParent API function.

In VB6 there is a limit of 255 separately named controls on one form. You usually don't run afoul of this restriction because 255 is a lot of controls. One case where you might have trouble is if you have a TabStrip with lots of controls on lots of tabs.

One way to get around the restriction is to use control arrays because the controls in an array don't count separately. In particular, you can often put all of your labels in one array.

This example uses another approach. It reparents PictureBoxes containing controls from other forms onto the form containing the TabStrip.

Windows draws the controls inside the new parent but you can still refer to the control on the original form in your Visual Basic code so that's how you interact with the control. The original form also contains the controls' event handlers.

This program makes an array m_PicTab(1 To m_MaxTab) of PictureBoxes to hold references to the controls loaded from the other forms.

When you click on a tab, the program sets Visible = False to hide the PictureBox on the currently selected tab. It then checks m_PicTab to see if the newly selected PictureBox has been loaded yet. If not, the program uses SetParent to move the control into the current form and postions the control.

After the control is loaded, the program makes it visible.

 
' Display the selected tab.
Private Sub TabStrip1_Click()
    ' Hide the previously selected tab.
    If m_SelectedTab >= 0 Then _
        m_PicTab(m_SelectedTab).Visible = False

    ' See which tab was clicked.
    m_SelectedTab = TabStrip1.SelectedItem.Index

    ' See if the tab is already loaded.
    If m_PicTab(m_SelectedTab) Is Nothing Then
        ' The tab is not yet loaded. Load it.
        ' Get a reference to the target control.
        Select Case m_SelectedTab
            Case 1  ' Home address.
                Set m_PicTab(m_SelectedTab) = _
                    frmTabHomeAddress.picTab
            Case 2  ' Business address.
                Set m_PicTab(m_SelectedTab) = _
                    frmTabBusinessAddress.picTab
        End Select

        ' Reparent the control.
        SetParent m_PicTab(m_SelectedTab).hwnd, Me.hwnd

        ' Put the control in the right position.
        With m_PicTab(m_SelectedTab)
            .Move _
                TabStrip1.Left + 120, _
                TabStrip1.Top + 360, _
                TabStrip1.Width - 240, _
                TabStrip1.Height - 480
            .ZOrder
        End With
    End If

    ' Make the tab visible.
    m_PicTab(m_SelectedTab).Visible = True
End Sub
 
The other important part of the program is Form_Unload. Here the program reparents any loaded controls back to their original forms. Windows can get confused if you close the forms without doing this.

Form_Unload also unloads the forms for any loaded controls so the program can end normally.

 
' Unload any loaded tabs.
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer

    For i = 1 To m_MaxTab
        If Not (m_PicTab(i) Is Nothing) Then
            Select Case i
                Case 1  ' Home address.
                    SetParent m_PicTab(i).hwnd, _
                        frmTabHomeAddress.hwnd
                    Unload frmTabHomeAddress
                Case 2  ' Business address.
                    SetParent m_PicTab(i).hwnd, _
                        frmTabBusinessAddress.hwnd
                    Unload frmTabBusinessAddress
            End Select
            Set m_PicTab(i) = Nothing
        End If
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated