How to make container dependent control?

2019-08-16 09:59发布

问题:

I am creating a Wizard Control in C# application. So, I have created two classes XWizardControl and XWinzardPage . The collection of XWizardPage will be created in XWizardControl. User can manually add number of XWizardPage in XWizardControl just like TabPage in TabControl. My problem is developer cannot use that XWizardPage control directly in any other container or form. They cannot add directly into any container. But they can create object from the editor and it should not be displayed in my ToolBox. So, I am confused in what attribute should i use to hide that PageControl in ToolBox and how to avoid user to add that control into any other container control.

Class Declaration as Follows: I don't want to make you confused. So, I am just placing declaration.

//Main Control
public partial class XWizardControl : DevExpress.XtraEditors.XtraUserControl
{
    public XWizardPageCollection Pages
    { get; set; }        
}

//Collection of WizardPage
class XWizardPageCollection : IEnumerable<XWizardPage>, ICollection<XWizardPage>

//Wizard Page
public partial class XWizardPage : DevExpress.XtraEditors.XtraPanel, IComparable<XWizardPage>
{
   //Some Properties
   public XWizardPage()  //Contructor
}

If you need more specification then please tell me. I will edit my question give that detail in UPDATE:

You can provide solution in VB also.

回答1:

  1. To hide a control from toolbox, add the ToolboxItemAttribute.
  2. To avoid a XWizardPage being parented to controls other than a XWizardControl (at design time) you'll need to create a custom ControlDesigner and override CanBeParentedTo.

Example

Public Class XWizardPageDesigner
    Inherits PanelDesigner

    Public Overrides Function CanBeParentedTo(ByVal parentDesigner As IDesigner) As Boolean
        Return ((Not parentDesigner Is Nothing) AndAlso TypeOf parentDesigner.Component Is XWizardControl)
    End Function

End Class

<ToolboxItem(False), Designer(GetType(XWizardPageDesigner))> _
Public Class XWizardPage
    Inherits DevExpress.XtraEditors.XtraPanel
    Implements IComparable(Of XWizardPage)

End Class

Update

Also, you may want to create a custom ControllCollection for the XWizardControl and override add so that only XWizardPage can be added.

Public Class XWizardControl
    Inherits DevExpress.XtraEditors.XtraUserControl

    Protected Overrides Function CreateControlsInstance() As Control.ControlCollection
        Return New XWizardControlCollection(Me)
    End Function

    Private Class XWizardControlCollection
        Inherits DevExpress.XtraEditors.XtraUserControl.ControlCollection

        Public Sub New(owner As XWizardControl)
            MyBase.New(owner)
        End Sub

        Public Overrides Sub Add(ByVal value As Control)
            If (Not TypeOf value Is XWizardPage) Then
                Throw New ArgumentException()
            End If
            MyBase.Add(value)
        End Sub

    End Class

End Class