How to Dynamically create controls at runtime using Controls.Add and place them in a scrolled window
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 'Controls - How to Dynamically create controls at runtime using Controls.Add and place them in a scrolled window 'Use the Controls collection's Add method to create the new control specifying 'the inner PictureBox as its parent. 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 ctl As TextBox Dim prev As TextBox ' Create the control. m_NumControls = m_NumControls + 1 Set ctl = Controls.Add("VB.TextBox", "Text" & Format$(m_NumControls), picInner) ' Position the control. Set prev = Controls("Text" & Format$(m_NumControls - 1)) ctl.Move prev.Left, prev.Top + prev.Height + 30, prev.Width, prev.Height ctl.Text = ctl.Name ' Size picInner to hold the control. picInner.Move 0, 0, ctl.Left + ctl.Width + 120, ctl.Top + ctl.Height + 120 ' Display the control. ctl.Visible = True ' Rearrange the scroll bars. ArrangeScrollBars End Sub |