I created a component that contains a collection editable at design-time. I need to apply the DesignTimeVisible attribute dynamically so that I can either add components directly to the form (and are visible) or add them through the collection editor (and are not visible).
The components implement a custom ComponentDesigner that overrides PreFilterAttributes(). This allows me to add the DesignTimeVisibleAttribute as the component is being created:
Public
Class MyComponentDesigner
Inherits ComponentDesigner
Protected Overrides Sub PreFilterAttributes(ByVal attributes As System.Collections.IDictionary)
MyBase.PreFilterAttributes(attributes)
Dim DesignTimeVisible As Boolean = CType(Me.Component, MyComponent).DesignTimeVisible
Dim DTV As New DesignTimeVisibleAttribute(DesignTimeVisible)
attributes.Add(GetType(DesignTimeVisibleAttribute), DTV)
End Sub
End Class
MyComponent thus contains a property called DesignTimeVisible. When set, TypeDescriptor.Refresh is called to update the visibility changes in the designer.
<Browsable(
False), _
DefaultValue(
GetType(Boolean), "True"), _
DesignOnly(
True)> _
Public Property DesignTimeVisible() As Boolean
Get
Return m_DesignTimeVisible
End Get
Set(ByVal value As Boolean)
m_DesignTimeVisible = value
TypeDescriptor.Refresh(
Me)
End Set
End Property
My collection is a custom collection inheriting from CollectionBase to support use of the collection editor. When I add a component to the collection, it sets the component's DesignTimeVisible property to false. This is how I control the condition of whether to make the component visible in the component tray.
Public
Class MyComponentCollection
Inherits CollectionBase
Private m_Parent As MyComponentContainer
Friend Sub New(ByVal Parent As MyComponentContainer)
m_Parent = Parent
End Sub
Public Function Add(ByVal Item As Object) As Object
CType(Item, MyComponent).Parent = Me
CType(Item, MyComponent).DesignTimeVisible = False
List.Add(Item)
Return Item
End Function
Public Sub AddRange(ByVal Items() As Object)
For Each Item As Object In Items
CType(Item, MyComponent).Parent = Me
CType(Item, MyComponent).DesignTimeVisible = False
List.Add(Item)
Next
End Sub
...
End Class
The effects of this implementation are new instances of MyComponent still show up in the designer component tray until the next time the designer refreshes. I'm pretty sure this happens because the designer overrides my changes with the default values as the component's core is created. To try and circumvent this problem, I tried implementing a custom ToolBoxItem and overriding CreateComponentsCore:
Protected Overrides Function CreateComponentsCore(ByVal host As System.ComponentModel.Design.IDesignerHost) As System.ComponentModel.IComponent()
Dim Component As New MyComponent()
' TODO: Now what?
host.Container.Add(Component)
Return New System.ComponentModel.IComponent() {Component}
End Function
This is where I am stuck. Is this the the piece of the puzzle I'm missing? Part of the problem here is I don't know if any criteria exists to check whether or not to change MyComponent.DesignTimeVisible to False. Whats the next step?
Pete