开发者

How to drop controls onto a panel in a user control?

I have created a UserControl that has 3 panels.

What I what to do is expose one of the pane开发者_Go百科ls to the Visual Studio designer so you can drop controls inside that panel.

Currently I have the UserControl on a form but dragging say a text box over it will have the text box paint over the UserControl and not be a part of the user control.


Surgery is required to make the nested panel designable. The steps are explained well in this blog post.


The blog post in @nobugz answer is excellent. Thought the following vb example may help others.

Create your user control as normal.

In this example the control name is HorizontalCollapsiblePanel and the ContentPanel is panel to be exposed to the designer.

Add designer attribute to the class statement.

<System.ComponentModel.Designer(GetType(HorizontalCollapsiblePanel.Designer))> _
Public Class HorizontalCollapsiblePanel

Create a readonly property to the panel to be exposed.

<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Content() As Panel
    Get
        Return Me.ContentPanel
    End Get
End Property

Create a class within the HorizontalCollapisblePanel class for use by the designer.

Public Class Designer
    Inherits System.Windows.Forms.Design.ControlDesigner

    Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
        MyBase.Initialize(component)

        EnableDesignMode(DirectCast(component, HorizontalCollapsiblePanel).Content, "Content")

    End Sub

End Class

Note that System.Windows.Forms.Design.ControlDesigner requires a reference to System.Design.dll.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜