NickD
NickD

Reputation: 191

Inserting a panel between 2 controls in Windows form

I want to insert a Panel (User Control) dynamically between two other control. Let say I have a PictureBox(Control) as the parent are there is an other control on the pictureBox.

I want to remove the other control from the picture box, add a panel(control) to the picture box and then set the older control to the new panel. For now I have this code :

   If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
     For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
        If (child.BackColor = Drawing.Color.Transparent) Then

           Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
           panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
           panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
           m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)

           panel.Controls.Add(child)
           m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
           AddHandler panel.Paint, AddressOf PanelPaintEvent
        End If
     Next
  End If

My code is adding a background wrapper to the transparent color child in front of it. The thing is even if I remove the child before adding or after adding it back, I can never see it on the screen. Is there any particular thing that maybe the Remove does so the removed Child isn't usable again ?

Upvotes: 1

Views: 252

Answers (1)

djv
djv

Reputation: 15774

You should change the child location to (0, 0), else it will still be relative to its position inside MainDoubleBufferedPictureBox, but now it's in the new panel. It could be there, just located outside the panel's boundary

If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
    For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
        If (child.BackColor = Drawing.Color.Transparent) Then

            Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
            panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
            panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
            m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)

            panel.Controls.Add(child)
            child.Location = New System.Drawing.Point(0, 0) ' <--- this
            m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
            AddHandler panel.Paint, AddressOf PanelPaintEvent
        End If
    Next
End If

Upvotes: 1

Related Questions