tags:

views:

211

answers:

9

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 parameter, given that it's already true by default.

+5  A: 

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.

SLaks
Why was this downvoted?
SLaks
Good explanation of where it would specifically be used.
itsmatt
+3  A: 

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

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.

Anton Gogolev
This doesn't answer the question.
SLaks
@SLaks: It answers the original question - just not your re-interpretation of it.
Dan Puzey
I'm pretty sure my interpretation is correct. (Otherwise, he would;t have said `true`)
SLaks
A: 

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.

Justin Niessner
A: 

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.

David
This doesn't answer the question.
SLaks
there, i added an answer.
David
This is wrong; there are cases where you do have to.
SLaks
A: 

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.

klausbyskov
It's `true` by default. You only need to specify the attribute when setting it to `false`.
SLaks
yes, but properties are by default browsable. I think the OP asked if there are cases where a property needs this set to *true* in order to appear.
Isak Savo
@SLaks and @Isak Save good points. I guess an attribute value of true makes sense when overriding a property in a parent class that has set it to false explicitly.
klausbyskov
A: 

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.
    }
 }
SwDevMan81
You don't need the attribute; the comment alone should be good enough.
SLaks
haha, that is true, but the browsable tells them exactly which attribute you are talking about
SwDevMan81
You could change the comment to `//Do NOT add [Browsable(false)]`
SLaks
A: 

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

Brian Scott
+1  A: 

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.

Josh Einstein
A: 

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; } }
}
Marc Gravell