ahazzah
ahazzah

Reputation: 776

Hide WinForm UserControl custom property from VS designer

Visual Studio is incorrectly calling my UserControl's custom properties at design time.

I have read many of the posting about using the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes, but this has not worked for me.

To reproduce this problem, using Visual Studio, create a new Windows Forms Application, then add a User Control to your project, and drag that User Control onto your Form. Add a public custom property to your User Control, as shown below.

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    [Browsable( false )]
    [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
    public bool AreYouThere
    {
        get
        {
            MessageBox.Show( "Yes I Am Here!" );
            return true;
        }
    }
}

When the Form is open in the Visual Studio designer, if I force the solution to clean and then rebuild, I will see a MessageBox with the text "Yes I Am Here!", indicating that Visual Studio has called the AreYouThere property on my User Control.

This should not happen, since I have decorated the AreYouThere property with the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes.

Any idea why this is happening?

(This problem occurs on Visual Studio 2010 and 2013).

Upvotes: 10

Views: 4585

Answers (3)

9jaCoder
9jaCoder

Reputation: 48

Do not set the default value for the property as you want it. In your example, set the property AreYouThere to false/true and in the parent or whereever you are using it you instanceOfUserControl1.AreYouThere = true/false in say Load event.

Upvotes: 0

Senior .Net Dev3loper
Senior .Net Dev3loper

Reputation: 111

public class CustomDesigner : ControlDesigner
{
    private static string[] RemovedProperties = new[]
    {
        "AccessibilityObject","AccessibleDefaultActionDescription","AccessibleDescription",
        "AccessibleName","AccessibleRole","AllowDrop","Anchor","AutoEllipsis","AutoScrollOffset",
        "AutoSize","AutoSizeMode","FlatAppearance", "FlatStyle",
        "TextAlign","TextImageRelation","UseCompatibleTextRendering",
        "UseMnemonic","UseWaitCursor"
    };

    public CustomDesigner() { }

    protected override void PreFilterProperties(IDictionary properties)
    {
        foreach (string prop in RemovedProperties)
        {
            properties.Remove(prop);
        }
        base.PreFilterProperties(properties);
    }
}

[ToolboxItem(true)]
[DesignerCategory("code")]
[Designer(typeof(CustomDesigner))]
public partial class NewButton : Button
{
    public Color OnHoverBackColor
    {
        get { return _onHoverBackColor; }
        set
        {
            _onHoverBackColor = value;
            Invalidate();
        }
    }
}

Upvotes: 0

Mihail Shishkov
Mihail Shishkov

Reputation: 15797

In order to hide a property from every place possible you have to mark it with those attributes

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]

Upvotes: 13

Related Questions