none
How to prevent a panel from absorbing independant controls in designer? RRS feed

  • Question

  • Hi!

    How do I prevent any objects in the dimensions of a panel on my form from becoming children of that panel. I want my objects to remain separate from the panel.

    Thanks in advance!

    Tuesday, October 24, 2017 8:52 PM

Answers

  •  Sure it is possible.  One way is to create the controls dynamically (in the code),  adding them to the form's controls at the position you want,  and then calling the BringToFront method of the control.  For example,  i placed a panel on the form in the designer and used this code...

    Public Class Form1
        Private WithEvents btn As New Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Panel1.AutoScroll = True
    
            btn.Bounds = New Rectangle(Panel1.Left + 10, Panel1.Top + 10, Panel1.Width - 34, 40) 'sets the button's location and size
            btn.Text = "Over The Panel"
            Me.Controls.Add(btn) 'adds the button to the Form's controls
            btn.BringToFront() 'brings it to the front of the z order, in front of any other controls
    
            'just adds some buttons in the scrollable panel for this example.
            For i As Integer = 0 To 20
                Dim b As New Button
                b.Text = "Button " & i.ToString
                b.Left = 10
                b.Top = i * (b.Height + 10)
                Panel1.Controls.Add(b)
            Next
        End Sub
    End Class
    

     

     

     

     You can also do this by adding the controls on form in the designer too.  Place a panel on the form,  then add a button to the form.  The button will be at the top of the z order because it was added after the panel.  Now move or adjust the size of the panel so you can place the button where you want it to be.  Last,  move or resize the panel back to its original position.  The button should be over the panel but,  not a control in the panel.

     However,  if you select and move the button just a little while it is over top of the panel,  it will get added to the panel's controls automatically and you will need to do it again.

      You can select controls in the designer,  right click them,  and select 'Move To Front' or 'Move To Back' in the designer too.  This moves them to the top or bottom of the z order.  Play around with it and see what it does on a new form project.


    If you say it can`t be done then i`ll try it

    Wednesday, October 25, 2017 1:38 AM
  • How do I prevent any objects in the dimensions of a panel on my form from becoming children of that panel.

    If you place the panel after you place the other control, then that other control will not be contained in the panel, even if the panel fully overlays it.

    Wednesday, October 25, 2017 2:08 AM

All replies

  • Is this even possible?
    Tuesday, October 24, 2017 11:35 PM
  •  Sure it is possible.  One way is to create the controls dynamically (in the code),  adding them to the form's controls at the position you want,  and then calling the BringToFront method of the control.  For example,  i placed a panel on the form in the designer and used this code...

    Public Class Form1
        Private WithEvents btn As New Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Panel1.AutoScroll = True
    
            btn.Bounds = New Rectangle(Panel1.Left + 10, Panel1.Top + 10, Panel1.Width - 34, 40) 'sets the button's location and size
            btn.Text = "Over The Panel"
            Me.Controls.Add(btn) 'adds the button to the Form's controls
            btn.BringToFront() 'brings it to the front of the z order, in front of any other controls
    
            'just adds some buttons in the scrollable panel for this example.
            For i As Integer = 0 To 20
                Dim b As New Button
                b.Text = "Button " & i.ToString
                b.Left = 10
                b.Top = i * (b.Height + 10)
                Panel1.Controls.Add(b)
            Next
        End Sub
    End Class
    

     

     

     

     You can also do this by adding the controls on form in the designer too.  Place a panel on the form,  then add a button to the form.  The button will be at the top of the z order because it was added after the panel.  Now move or adjust the size of the panel so you can place the button where you want it to be.  Last,  move or resize the panel back to its original position.  The button should be over the panel but,  not a control in the panel.

     However,  if you select and move the button just a little while it is over top of the panel,  it will get added to the panel's controls automatically and you will need to do it again.

      You can select controls in the designer,  right click them,  and select 'Move To Front' or 'Move To Back' in the designer too.  This moves them to the top or bottom of the z order.  Play around with it and see what it does on a new form project.


    If you say it can`t be done then i`ll try it

    Wednesday, October 25, 2017 1:38 AM
  • Incredible, thank you!
    Wednesday, October 25, 2017 1:49 AM
  • How do I prevent any objects in the dimensions of a panel on my form from becoming children of that panel.

    If you place the panel after you place the other control, then that other control will not be contained in the panel, even if the panel fully overlays it.

    Wednesday, October 25, 2017 2:08 AM
  • Incredible, thank you!

     You're Welcome.  8)

    If you say it can`t be done then i`ll try it

    Wednesday, October 25, 2017 2:08 AM