HOWTO: Use SetParent to Place Controls on New Tab at Run-Time

ID: Q161828


The information in this article applies to:


SUMMARY

In Visual Basic 5.0, you can use the SetParent API to place controls on a newly-created tab of the SSTab control. You can make these controls invisible children of the form on which the SSTab control is located at design-time and make them children of the SSTab control at run-time. Before removing the tab, you must remove the children controls. You can remove them by once again making the controls children of the form parent with the SetParent API.

In Visual Basic 6.0, this technique is not necessary: you can create controls at run-time.


MORE INFORMATION

  1. Start a new Visual Basic 5.0 Project. In the New Project dialog box, select Standard Exe and click Open. Form1 appears by default.


  2. On the Project menu, click Components. Select "Microsoft Tabbed Dialog Control 5.0." This makes the SSTab control part of your toolbox.


  3. Place the following objects on Form1 and set the appropriate properties:
    
       Control             Name              Property         Value
       ------------------------------------------------------------------
       SSTab               SSTab1            (Name)          SSTab1
                                             Tabs            3
                                             TabsPerRow      5
    
       Command Button      Command1          (Name)          Command1
                                             Caption         Add Tab
    
       Command Button      Command2          (Name)          Command2
                                             Caption         Remove Tab
    
       Command Button      Command3          (Name)          Command3
                                             Caption         Button on Tab 3
                                             Visible         False
    
       ListBox             List1             (Name)          List1
                                             Visible         False
     


  4. Insert a basic module and type the following declare statement in the General Declarations section:
    
          Declare Function SetParent Lib "user32" (ByVal hWndChild As Long,
          ByVal hWndNewParent As Long) As Long
     


  5. Copy the following code to the Form1 Code Window:
    
          Private Sub Command1_Click()
    
              SSTab1.Tabs = 4 'Add a tab to the control
              SSTab1.Tab = 3       'Bring this tab to foreground
              x& = SetParent(List1.hWnd, SSTab1.hWnd) 'Make tab parent to list1
              List1.Visible = True    'Make list1 visible
              List1.Move SSTab1.Width \ 10, SSTab1.Height \ 3, SSTab1.Width \ _
                 3, SSTab1.Height \ 2    'Move list1 to an appropriate location
              'Populate ListBox
              For i% = 1 To 10
                  List1.AddItem Str$(i%) & " This is an item in List1"
              Next i%
              x& = SetParent(Command3.hWnd, SSTab1.hWnd) 'Make tab parent to
                                                         'command3
              Command3.Visible = True 'Make command3 visible
              Command3.Move SSTab1.Width - SSTab1.Width \ 3, SSTab1.Height \ _
                 3, SSTab1.Width \ 4, SSTab1.Height \ 4 'Move command3 to an
                                                        'appropriate position
          End Sub
    
          Private Sub Command2_Click()
    
           'You need to remove controls from the tab before removing the tab
           List1.Clear 'Clear the ListBox
           x& = SetParent(List1.hWnd, Form1.hWnd) 'Make the ListBox a child of
                                                  'the form
           List1.Visible = False
           List1.Move Form1.Width * 0.9, Form1.Height \ 10, Form1.Width \ 10, _
                      Form1.Height \ 10
           x& = SetParent(Command3.hWnd, Form1.hWnd) 'Make command3 a child of
                                                     'the form
           Command3.Visible = False
           Command3.Move Form1.Width * 0.9, Form1.Height * 0.3, Form1.Width \ _
                         10, Form1.Height \ 10
           SSTab1.Tabs = 3 'Remove the tab from the control
           SSTab1.Tab = 2 'Bring tab 2 to the foreground
    
          End Sub
    
          Private Sub Command3_Click()
               MsgBox "The command button on the tab control is functional"
          End Sub
     


  6. Run the program. Click Add Tab. This creates a tab, and places a ListBox and Command Button on it. The ListBox is populated with 10 items. When you click "Button on tab3," a message box appears with the following message:

    "The Command Button on the tab control is functional."


  7. Click Remove Tab to remove the tab. The ListBox and Command Button are once again invisible and become children of the form.


Additional query words: kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbControl


Keywords          : 
Version           : 
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 16, 1999