Results 1 to 9 of 9

Thread: Custom Panel Not Acting as Parent to Contained Controls

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Custom Panel Not Acting as Parent to Contained Controls

    I created a simple custom controlled Panel for double buffering. When I use the control in my toolbox it works just fine, but when I attempt to add controls inside the panel those controls aren't staying when I move the panel around. My custom panel isn't acting as the controls' parent. I verified this by looking in the designer code.

    I tested a standard panel on the same form and it works perfectly normal - as I drag the parent Panel the interior controls move along with it. It is only my custom Panel which doesn't seem to detect when another control is placed within it.

    Here is my constructor code:

    Code:
    Public Class DBPanel
        Inherits System.Windows.Forms.PictureBox
        Public Sub New()
            MyBase.New()
    
            SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.ResizeRedraw, True)
            Me.UpdateStyles()
        End Sub
    End Class
    What am I missing to get this control to behave like a regular panel?

    Thanks!
    Intermediate Level Programmer Extraordinaire

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Custom Panel Not Acting as Parent to Contained Controls

    PictureBoxes aren't intended to be parent (container) controls.
    could you use a panel + change controlstyles in that?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Custom Panel Not Acting as Parent to Contained Controls

    something like this:

    vb Code:
    1. Public Class DBPanel
    2.     Inherits Panel
    3.  
    4.     Public Sub New()
    5.         MyBase.New()
    6.  
    7.         SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
    8.         SetStyle(ControlStyles.ResizeRedraw, True)
    9.         Me.UpdateStyles()
    10.     End Sub
    11.  
    12.     Private _image As Image
    13.     Public Property Image() As Image
    14.         Get
    15.             Return _image
    16.         End Get
    17.         Set(ByVal value As Image)
    18.             _image = value
    19.             Me.Invalidate()
    20.         End Set
    21.     End Property
    22.  
    23.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    24.         If Me.Image IsNot Nothing Then
    25.             e.Graphics.DrawImage(Me.Image, Me.Bounds) 'stretch image
    26.         End If
    27.         MyBase.OnPaint(e)
    28.     End Sub
    29.  
    30. End Class

  4. #4

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Custom Panel Not Acting as Parent to Contained Controls

    I cut an pasted the wrong custom control code - this is from my custom picturebox (which works fine). If you just replace "Inherits System.Windows.Forms.PictureBox" with "Inherits System.Windows.Forms.Panel" that would make my code accurate.

    I truly apologize for the error!
    Intermediate Level Programmer Extraordinaire

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Custom Panel Not Acting as Parent to Contained Controls

    ok. i tried it. it worked ok for me.
    is there any other code involved?

  6. #6

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Custom Panel Not Acting as Parent to Contained Controls

    Thanks for your efforts. The only relevant code I can think of is in the designer. What is see is this:
    Code:
            Me.Controls.Add(Me.DbPanel7)
            Me.Controls.Add(Me.Label3)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.Label1)
    DbPanel is the custom panel and the labels are supposed to be inside the panel. It should look like this:
    Code:
            Me.Controls.Add(Me.DbPanel7)
            Me.DbPanel7.Controls.Add(Me.Label3)
            Me.DbPanel7.Controls.Add(Me.Label2)
            Me.DbPanel7.Controls.Add(Me.Label1)
    No matter where I place these labels the designer falls to have them contained in my panel. I have learned the hard way never to mess with designer code, so I haven't attempted to change anything.

    Very frustrating...
    Intermediate Level Programmer Extraordinaire

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Custom Panel Not Acting as Parent to Contained Controls

    did you add them to the form + drag them onto the panel?
    all i can suggest is that you make sure the panel is focused when adding controls.

    your 2nd snippet is how the designer should be + it should be ok to go ahead + change it.

  8. #8

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Custom Panel Not Acting as Parent to Contained Controls

    When I switched the code in the designer it switched it right back! It's almost funny how badly this code does not want to work.

    One other interesting bit of information I just noticed - my custom panels do not have 4-way arrow icons in the upper left corner that regular panels possess. Whereas in a regular panel you have to drag and drop using this icon my panels can be dragged and dropped from anywhere inside the panel. Does this provide a hint?
    Intermediate Level Programmer Extraordinaire

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Custom Panel Not Acting as Parent to Contained Controls

    it adds to the evidence that there's something wrong, but it doesn't lead to an easy solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width