When do we need to use [Browsable(true)]
?
EDIT (by SLaks): He\'s asking (I assume) why one would need to pass true
as the
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; } }
}
As far as I know, never.
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.
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.
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
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.
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.