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
 
 
 
 
 
 
TitleDynamically create controls at runtime using Load and place them in a scrolled window
KeywordsLoad, dynamic control, runtime
CategoriesControls
 
Use Load to create the new control. Then use the SetParent API function to move it into a PictureBox. Use scroll bars to allow the user to move the PictureBox inside another PictureBox to make the inner one scroll.
 
' Add a new control to the inner PictureBox.
Private Sub mnuControlsAdd_Click()
Dim index As Integer

    ' Create the control.
    index = Text1.Count
    Load Text1(index)

    ' Reparent the control so it's inside
    ' the inner PictureBox.
'    SetParent Text1(index).hWnd, picInner.hWnd

    ' Martijn Coppoolse <vbhelper.com@martijn.coppoolse.com>
    ' pointed out that setting the Container is
    ' easier than reparenting.
    Set Text1(index).Container = picInner

    ' Position the control.
    Text1(index).Top = Text1(index - 1).Top + _
        Text1(index - 1).Height + 30
    Text1(index).Text = "Text1(" & Format$(index) & ")"

    ' Size picInner to hold the control.
    picInner.Move 0, 0, _
        Text1(index).Left + Text1(index).Width + 120, _
        Text1(index).Top + Text1(index).Height + 120

    ' Display the control.
    Text1(index).Visible = True

    ' Rearrange the scroll bars.
    ArrangeScrollBars
End Sub
 
For a similar example that uses Controls.Add to make new controls instead of using a control array, see How To: Dynamically create controls at runtime using Controls.Add and place them in a scrolled window.

For details on making a scrolled window, see How To: Make a scrolled window.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated