Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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,



Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 30, 2008 8:16 AM

igorbrejc.net » Friday Goodies - 01. February said:

Pingback from  igorbrejc.net » Friday Goodies - 01. February

# February 1, 2008 1:57 PM

G. Vijay Bhargav said:

Very good article.. its informative..

# September 6, 2008 7:48 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!