21

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.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
Narmatha Balasundaram
  • 867
  • 3
  • 11
  • 26

9 Answers9

27

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.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
20

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.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
5

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
  • 48,814
  • 22
  • 151
  • 184
  • 1
    You don't need the attribute; the comment alone should be good enough. – SLaks Mar 25 '10 at 13:18
  • haha, that is true, but the browsable tells them exactly which attribute you are talking about – SwDevMan81 Mar 25 '10 at 13:19
  • 1
    You could change the comment to `//Do NOT add [Browsable(false)]` – SLaks Mar 25 '10 at 13:20
  • I just wonder why you would want to enforce the visibility of a property in the property pane to inheritors. So putting a comment to not inherit with `[Browsable(false)]` sounds a bit silly to me. – Xilconic Nov 28 '13 at 12:46
4

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

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
  • 242,243
  • 40
  • 408
  • 536
  • 2
    'never .. except..' is not right/complete, mainly overriding base class that has a value `false`. – EricG Jul 30 '14 at 07:42
1

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
  • 9,221
  • 6
  • 47
  • 68
1

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
  • 68,005
  • 14
  • 144
  • 156
  • The Xamarin iOS designer defaults to properties being non-designable. You must specify [Browsable(true)] for them to appear in the property panel. https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_overview/#Custom_Properties – chkn May 31 '17 at 21:48
0

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 Fox
  • 10,603
  • 9
  • 50
  • 80
0

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.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • It's `true` by default. You only need to specify the attribute when setting it to `false`. – SLaks Mar 25 '10 at 13:15
  • 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 Mar 25 '10 at 13:16
  • 1
    @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. – Klaus Byskov Pedersen Mar 25 '10 at 13:28