在Windows窗体中的2个控件之间插入一个面板

时间:2017-08-08 14:44:58

标签: vb.net winforms

我想在另外两个控件之间动态插入一个Panel(用户控件)。假设我有一个PictureBox(Control),因为父框架上有另一个控件。

我想从图片框中删除其他控件,将一个面板(控件)添加到图片框中,然后将旧控件设置为新面板。现在我有这个代码:

   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

我的代码是在前面的透明颜色子项中添加背景包装器。即使我在添加之前或之后删除了孩子,我仍然无法在屏幕上看到它。是否有任何特殊的东西,可能删除这样做被删除的孩子不能再次使用?

1 个答案:

答案 0 :(得分:1)

您应该将子位置更改为(0,0),否则它仍将相对于其在MainDoubleBufferedPictureBox中的位置,但现在它位于新面板中。它可能就在那里,只是位于面板边界之外

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