Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide WinForm UserControl custom property from VS designer

Tags:

c#

winforms

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).

like image 537
ahazzah Avatar asked Jun 13 '14 20:06

ahazzah


1 Answers

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)]
like image 140
Mihail Shishkov Avatar answered Oct 22 '22 04:10

Mihail Shishkov