Search code examples
.netvb.netpropertiesvisual-studio-addinsvisible

How to make a Visual Studio addin set a control's Visible property in the designer


I have written a Visual Studio 2008 addin that adds controls to a Form. I want some of those controls' Visible property set to False so they're hidden during runtime, so I do this:

If hiddenControls.Contains(.ColumnName) Then 'hiddenControls is TypeOf List(Of String)
    fieldControlAsControl.Visible = False 'TypeOf Control
End If

This doesn't work. Not only is the control invisible in the designer window itself, but the .Visible = False code doesn't even make it into [FormName].designer.vb.

I have tried forcing Serialization on the Visible property like so, to no avail:

<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Shadows Property Visible() As Boolean
    Get
        Return MyBase.Visible
    End Get
    Set(ByVal value As Boolean)
        MyBase.Visible = value
    End Set
End Property

Can anyone help me with forcing the Visible property to be serialized in my addin?


Solution

  • I found a relatively good workaround ('good' meaning it doesn't feel very ineloquent). I added the following code to the controls that get added to the form by my addin:

    <System.ComponentModel.Browsable(False)> _
    Public Overloads Property Visible() As Boolean
        Get
            Return MyBase.Visible
        End Get
        Set(ByVal value As Boolean)
            MyBase.Visible = value
        End Set
    End Property
    
    <System.ComponentModel.Category("Appearance")> _
    <System.ComponentModel.Description("Whether the FieldControl will be visible at runtime.")> _
    <System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    <System.ComponentModel.Browsable(True)> _
    Public Property VisibleAtRunTime() As Boolean
        Get
            Return mVisibleAtRunTime
        End Get
        Set(ByVal value As Boolean)
            mVisibleAtRunTime = value
            If Not DesignMode Then
                Visible = value
            End If
        End Set
    End Property
    

    I then have the addin set the "VisibleAtRunTime" property instead of the "Visible" property.