Creating a control with a Panel on it.

Jay1b

Member
Joined
Dec 12, 2007
Messages
21
Programming Experience
5-10
Hi

I'm trying to create a user control with a panel component on it. I can create a control which inherits from a panel, this allows me to drag on other controls during design time. But if i set them to fill they consume the whole control. Where I want sections of the control which the user can and cant drag other components onto at design time.

Thanks.
 
I'm passing along some wisdom that was once passed along to me ( as in this morning).


VB.NET:
<System.ComponentModel.DesignerAttribute(GetType(PanelingScrollableControlDesigner))> _
Public Class Paneling
inherits system.windows.forms.panel


end class


Public Class PanelingScrollableControlDesigner
    Inherits System.Windows.Forms.Design.ScrollableControlDesigner

    Public Overrides Function CanParent(ByVal control As System.Windows.Forms.Control) As Boolean


****Code to determine if the control was placed in a region that allows the control


        If control.location.x >= Whatever _
AND control.location.y >= Whatever  _
 Then
            Return MyBase.CanParent(control)
        Else
            Return False
        End If

************************************************


    End Function

End Class

Basically, use the area I marked off to determine whether or not that particular area of the control should allow the control to be placed there.
 
CanParent only controls what can be added to the container, not what happens next (so you can Dock-Fill afterwards)... but you can use it to disallow adding controls to the usercontrol itself and only allow for child containers.

If your usercontrol requires a sectioned layout you should use the available layout controls to define these areas. You have all those in "Containers" of Toolbox available to do this; Panel, Groupbox, FlowLayoutPanel, SplitContainer, TableLayoutPanel and TabControl. When you have set up the sections you can use the "Layout" section in properties window to define the Layout initially with behaviour when resize occurs, for example there is Anchor, Dock, MinimumSize and MaximumSize that may help to maintain the dynamic layout between your sections.

Here is a code sample translated to VB.Net from blog post How can I drop controls within UserControl at Design time? that show necessary amendments:
VB.NET:
Imports System.Windows.Forms.Design
Imports System.ComponentModel
<Designer(GetType(MyUserControlDesigner))> _
Public Class UserControl2

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property LeftPanel() As Panel
        Get
            Return Me.Panel1
        End Get
    End Property
End Class

Public Class MyUserControlDesigner
    Inherits ParentControlDesigner
    Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
        MyBase.Initialize(component)
        Dim uc As UserControl2 = DirectCast(component, UserControl2)
        'EnableDesignMode(uc.LeftPanel, "LeftPanel")
        Dim dh As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
        dh.Container.Add(uc.LeftPanel, "LeftPanel")
    End Sub  
End Class
You have to reference System.Design.dll to use these classes.
 
Back
Top