Narmatha Balasundaram
Narmatha Balasundaram

Reputation: 877

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

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.

Upvotes: 22

Views: 32104

Answers (9)

SwDevMan81
SwDevMan81

Reputation: 50028

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

Upvotes: 5

Anton Gogolev
Anton Gogolev

Reputation: 115867

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.

Upvotes: 20

Marc Gravell
Marc Gravell

Reputation: 1064124

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

Upvotes: 4

Josh
Josh

Reputation: 69282

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.

Upvotes: 1

Brian Scott
Brian Scott

Reputation: 9381

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

Upvotes: 1

SLaks
SLaks

Reputation: 888185

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.

Upvotes: 27

Justin Niessner
Justin Niessner

Reputation: 245489

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.

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121057

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.

Upvotes: 0

David Fox
David Fox

Reputation: 10753

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.

Upvotes: 0

Related Questions