简体   繁体   中英

Hide WinForm UserControl custom property from VS designer

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

Add a public custom property<\/strong> 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;
        }
    }
}

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)]
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();
        }
    }
}

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

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM