Propert Grid in the 2.0 Framework

Published Fri, Apr 22 2005 22:07 | William

Like a whole lot of other things in the 2.0 Framework, stuff that used to be a pain in the a55 got a whole lot simpler.  Using the PropertyGrid control is probably on of the easiest of them.  Basically, if you want to provide a clean and easy way for people to edit properties of your objects, all you need to do is drag one of these bad boys on a form and then set the SelectedObject property to your object. Every public property that isn't otherwise marked is browsable. If there's a get and set accessor, you can set the properties in addition to viewing them.  If you only have get accessors, then the property will be disabled in the PropertyGrid.  To let the user select a control, I used the following snippet (couldn't be easier).  The main reason I built this thing was that I was debugging some code and I kept forgetting to set properties that I needed.  I ended up doing it in the debug window but that leaves a little to be desired w/ screen refreshes and all. For the sake of illustration, I have the property grid show from behind a button click, but what I actually implemented was this via a ContextMenue.  However you implement it, it's easy as h3ll.

foreach (Control ctrl in this.Controls)
{
this.listBox1.Items.Add(ctrl.Name);


}
 
So to make things easy, I just created a Form, dropped a PropertyGrid onto it, set the Dock property to full, and viola.

 

Again, this is totally simple but it's pretty cool for editing your objects.  Note that in the listbox, I listed only controls on the form - but I could just as easily have loaded configuration values, other controls, or any other object that I so desired.  All that I do  is create an overloaded constructor and set the SelectedObject property to whateverI pass in .  Since you can pass in ANY object, you have tremendous flexibility:

public Form2(object o)

{

InitializeComponent();

this.propertyGrid1.SelectedObject = o;

}

Comments

# William said on April 24, 2005 7:43 AM:

It is quite cool they surfaced this to be added by default (I commented on it in my february ctp post).

Just a fyi, VS.NET 2003 offers it too:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/usingpropgrid.asp

Cheers
Daniel
(still waiting for that VS2005 solution you promised, although I must admit I haven't tried it with B2 yet ;-)

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications