Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Using System.ComponentModel Attributes to your advantage

The System.ComponentModel namespace (more information here) in .Net contains an array of various attributes that every .Net developer should know about.  These attributes provide a way for the developer to implement design-time and run-time behavior on both controls and components.  In addition to attributes, this namespace also has base classes, interfaces, binding sources, etc.

By using the various attributes in this namespace you can create better, more useable UI controls for your projects.  Below are a few that I tend to use often.

BrowsableAttribute - (more info here)
This is a boolean attribute and when set to false, the property on the control will not show up in the Properties panel in the IDE.  This is nice when you want to have a property but not expose it at design time to the developer.

CategoryAttribute - (more info here)
This is a simple attribute that will take a string and will allow you to create a custom category for your control.  If you do not use this, your control property will show up in the 'Misc' category in the Properties panel in the IDE.

DefaultValueAttribute - (more info here)
This attribute will all you to set the default value for the property in the control itself.  This will also show the default value in the Properties panel in the IDE. 

NOTE - This WILL NOT work if you also are using the DesignOnly attribute.

DescriptionAttribute - (more info here)
This is another simple property that will allow the author of the control to provide description text about the meaning of the property.  This is similar to adding comments to the code, but it will be displayed in the Properties panel in the IDE.

DesignOnlyAttribute - (more info here)
This is a boolean attribute that is great.  You this and set it to true if you have a property that you DO NOT WANT to be set with any type of default value in the designer generated code.  Adding this can save you a ton of time and effort.

NOTE - My experience has shown me that this will supercede the DefaultValue attribute is this is set to true.

ReadOnlyAttribute - (more info here)
This attribute is just a way to mark the property as read only.  This can also be accomplished by NOT putting a setter on the property.

Till next time,

Posted 01-30-2008 7:03 AM by Derik Whittaker


Comments wrote Using System.ComponentModel attributes to your advantage
on 01-30-2008 8:16 AM

You've been kicked (a good thing) - Trackback from » Friday Goodies - 01. February wrote » Friday Goodies - 01. February
on 02-01-2008 1:57 PM

Pingback from » Friday Goodies - 01. February

G. Vijay Bhargav wrote re: Using System.ComponentModel Attributes to your advantage
on 09-06-2008 7:48 AM

Very good article.. its informative..

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)