Add entire contents from a form to a panel

Hi, i want to add alle the controls from a form to a panel on another form (sort of an MDI app), the method i would like to be using doesn't seem to get all the controls, and furthermore, it seems that the controls are removed from the collection - the method is here:

For Each ctl As Control In FormObject.Controls

newPanel.Controls.Add(ctl)

Next

Another method, which i'm using now, has to add the controls manually by getting the types of all objects, and thereafter creating them and setting the values correspondingly:

For Each objControl As Control In FormObject.Controls

Dim newControl As Control = Nothing

If TypeOf objControl Is Label Then

newControl = New Label

ElseIf TypeOf objControl Is TextBox Then

newControl = New TextBox

ElseIf TypeOf objControl Is DateTimePicker Then

newControl = New DateTimePicker

End If

If Not newControl Is Nothing Then

newPanel.Controls.Add(newControl)

newControl.Location = objControl.Location

newControl.Size = objControl.Size

newControl.Text = objControl.Text

newControl.Show()

End If

Next

Any good ideas would be appreciated... Thanks in advance!




Answer this question

Add entire contents from a form to a panel

  • hedelein

    Well i'm trying to make a tabbed MDI like usercontrol like the one in VS2005, where you're able to add childform-contents at runtime, and i'm basically looking for a method to copy all controls and handlers to panels on this usercontrol, without having to make all those "If TypeOf control is..." statements...

  • iainmcc

    why not create a user control consisting of all the controls on a form and then you can use it on both the original form and a panel on the other form. all the same layout and functionality.
  • Dmitry Lomov

    I used this procedure to to move all the controls from a tabpage to a form. 

     

     Private Sub OnteoraTabControl_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
            Dim pt As Point
            Dim x As Integer
            Dim tp As TabPage

            pt = Me.PointToClient(Cursor.Position)

            For x = 0 To Me.TabCount - 1
                Dim r As Rectangle = Me.GetTabRect(x)

                If r.Contains(pt) Then
                    intTab = x
                End If
            Next

            tp = Me.TabPages.Item(intTab)

            Dim frm As New frmFloatTab(Me)
            Dim ctrl As Control

            frm.Text = tp.Text
            frm.Panel1.BackColor = tp.BackColor
            frm.Panel1.ForeColor = tp.ForeColor

            For Each ctrl In tp.Controls
                ctrl.Parent = frm.Panel1
            Next

            If tp.ImageIndex >= 0 Then
                Dim bm As Bitmap = Me.ImageList.Images(tp.ImageIndex)
                frm.Icon = Icon.FromHandle(bm.GetHicon)
                frm.Tag = tp.ImageIndex
            End If

            frm.Show()
            frm.BringToFront()
            Me.TabPages.Remove(tp)
        End Sub



  • Add entire contents from a form to a panel