How Do I Hide the Properties of My Server Control From the Property Grid?

By default, all properties defined in your control will show up in the property grid when your control is selected in a designer like Visual Studio .NET or the Web Matrix. Often this is exactly what you want, but sometimes it doesn't make sense to have your properties displayed in the grid. For example, a read-only property isn't really useful to display; you should be able to access the property's value using code, but it's not necessary to display it in the grid.
Hiding the property form the grid is very easy. All you need to do is apply the BrowsableAttribute, like this:

  /// <summary>
  /// The UserName defines the user's name and is read-only.
  /// </summary>
  public string UserName
      return userName;

Note that this will work with other properties as well; not just with read-only properties.

Now the property will not show up in the property grid for your control, but you can still access it through code, e.g.:

Label1.Text = MyCustomControl.UserName;

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article.

Doc ID 308
Full URL
Short cut
Written by Imar Spaanjaars
Date Posted 06/29/2004 12:27
Date Last Reviewed 06/05/2006 21:34
Listened to when writing Come With Us by The Chemical Brothers (Track 1 from the album: Come With Us)

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.