VS2008:设计时嵌套的自定义组件

时间:2009-09-04 12:16:51

标签: vb.net

我有一个自定义组件,其中包含其他组件的列表。

如果我将一个子组件添加到列表中,它将显示在与文档大纲窗口中的父组件相同的级别上。

如何将其作为父组件的子项? (类似于例如TabControl的子项目的TabPages)

这是我的代码:

Public Class SomeComponent
    Inherits Component

    Public Sub New(ByVal cont As IContainer)
        cont.Add(Me)
    End Sub

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    <Editor("System.ComponentModel.Design.CollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(UITypeEditor))> _
    Public ReadOnly Property Items() As List(Of SomeOtherComponent)
        Get
            If _items Is Nothing Then
                _items = New List(Of SomeOtherComponent)
            End If
            Return _items
        End Get
    End Property
    Private _items As List(Of SomeOtherComponent) = Nothing

End Class

Public Class SomeOtherComponent
    Inherits Component

    Public Sub New()
    End Sub

    Public Sub New(ByVal cont As IContainer)
        cont.Add(Me)
    End Sub

    '...

End Class

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,这里是对原始代码的更改:

<Designer(GetType(SomeComponentDesigner))> _
Public Class SomeComponent
...
End Class

'this hides SomeOtherComponent from the component tray
<DesignTimeVisible(False)> _
Public Class SomeOtherComponent
...
End Class

Public Class SomeComponentDesigner
    Inherits ComponentDesigner

    Public Overrides ReadOnly Property AssociatedComponents() As System.Collections.ICollection
        Get
            If TypeOf (Me.Component) Is SomeComponent Then
                Return DirectCast(Me.Component, SomeComponent).Items
            Else
                Return MyBase.AssociatedComponents
            End If
        End Get
    End Property

End Class