When do we need to use [Browsable(true)]?

筅森魡賤 提交于 2019-11-30 11:15:37
Anton Gogolev

MSDN says it all:

Specifies whether a property or event should be displayed in a Properties window.

For example, if you're creating a User Control, you might want to decorate non-UI-related properties with [Browsable(false)] so that they will not be available through a "Properties" window.

Additionally, it controls which properties of an object can be seen in a PropertyGrid.

As for why we can pass true explicitly, I believe this is due to BrowsableAttributes property of a PropertyGrid. You can set it to contain BrowsableAttribute.No, so that the property grid will display all non-browsable members.

As far as I know, never.

EDIT

I was wrong.
It's necessary if you want to make a property which has [Browsable(false)] in your base class (such as UserControl.Text) browsable.

The problem is that things are browsable by default. The only scenario I can think where this would matter is overriding a member and changing the browsability... here F is visible only because of the [Browsable(true)] in the derived class - without it, it isn't visible.

using System.ComponentModel;
using System;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form { Controls = {new PropertyGrid {
            Dock = DockStyle.Fill, SelectedObject = new Bar()
        }}});
    }
}
public class Foo
{
    public virtual string A { get; set; }
    public virtual string B { get; set; }
    public virtual string C { get; set; }
    [Browsable(false)] public virtual string D { get; set; }
    [Browsable(false)] public virtual string E { get; set; }
    [Browsable(false)] public virtual string F { get; set; }
    [Browsable(true)] public virtual string G { get; set; }
    [Browsable(true)] public virtual string H { get; set; }
    [Browsable(true)] public virtual string I { get; set; }
}
public class Bar : Foo
{
    public override string A { get { return base.A; } set { base.A = value; } }
    [Browsable(false)] public override string B { get { return base.B; } set { base.B = value; } }
    [Browsable(true)] public override string C { get { return base.C; } set { base.C = value; } }
    public override string D { get { return base.D; } set { base.D = value; } }
    [Browsable(false)] public override string E { get { return base.E; } set { base.E = value; } }
    [Browsable(true)] public override string F { get { return base.F; } set { base.F = value; } }
    public override string G { get { return base.G; } set { base.G = value; } }
    [Browsable(false)] public override string H { get { return base.H; } set { base.H = value; } }
    [Browsable(true)] public override string I { get { return base.I; } set { base.I = value; } }
}

Probably when you want to make damn sure no one changes it :P

// I want to see this, dont change it to false or I'll hunt you down...
[Browsable(true)]
public int MyProperty {
   get {
      // Insert code here.
      return 0;
   }
   set {
      // Insert code here.
   }
}

The types and attributes in ComponentModel are not specifically tied to any particular designer. Although I don't know of any specific scenario that you would need to "opt in" to being designer-browsable, I suppose it's conceivable that you could have some component designer that would assume browsable(false).

I suppose you could also override a virtual property that specified browsable(false) and apply browsable(true) in the overridden member.

A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. These members can be modified at design time. Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. The default is true.

so, the answer is you never have to, as it is done by default.

According to the documentation you want it to be true when it should be displayed in the property window in VS. Basically it applies to classes that are used in the designer.

BrowsableAttribute Class (System.ComponentModel)

The documentation states:

A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true.

[Browsable] also defaults to true.

...so technically, you never need [Browsable(true)] unless you want to be very explicit.

One occassion when this attribute becomes important is during WebPart development for Sharepoint. In this scenario you are providing meta information for Sharepoint to determine whether your webpart should be viewable for selection etc. There are other similiar attributes such as Category and FriendlyName etc which are also taken into account.

See the following for examples:

Creating a web part with custom properties

And another with decent images of the sharepoint webpart editor which reflects your attributes:

Making Sharepoint WebParts interact

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!