2009-09-04 45 views
0

我有一個自定義組件,其中包含其他組件的列表。VS2008:設計時嵌套自定義組件

如果我將一個子組件添加到列表中,它將顯示在與文檔大綱窗口中父組件相同的級別上。

我怎樣才能使它成爲父組件的子項? (類似於例如是一個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 

回答

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