Follow @RoyOsherove on Twitter

System.Windows.Forms.PropertyGrid coolness

OK. maybe I'm a little slow, but has anyone else tried using the System.Windows.Forms.PropertyGrid  control ? It needs to be referenced from the "Customize toolbox" menu of the "tools" tool window.

I'm impressed. Basically, it has the same functionality as the property Grid of the VS.NET IDE. You Assign the .SelectedObject property of the grid to any of your objects, and it uses reflection to display it's properties. It even lets you manage public collections exposed by the object using a seperate window (just like the VS.NET property grid..) and it's all out of the box functionality. One line of code gets you there and it's pretty customizable as well.  coolness.

One quirk i came across: If you want it to manage your object's public collection of other objects, The object that is added to the collection must expose a default constructor(no params) which allows the default grid designer to create "new" items in the collection and then let you set properties for new items at run time visually.

update:

err. did that make any sense at all?

what i meant in the last paragraph was this: Say you have an object of type "Bank" which has a public collection of "Client" objects named "BankClients". Once you set the "SelectedObject" property of the propertygrid, In the property grid you would see a "BankClients" entry which is described as "collection" and has a little button with 3 dots on it.  if you click that button, another dialog opens up and lets you manage the clients collection at run time, letting you add, update and remove client objects from the collection.  The quirk above means a Client object must have a default contructor, since in the dialog you get a "Add" button which is supposed to instansiate a new Client object and add it to the collection and then show it visually. then you can set the new objects properties in a property grid that appears in the dialog next to the object's name.

However, since the dialog does not know(or does not care) of any non-default contructors, it will try to always call a default contructor of the object you want to add. if it has none, you get a nice little message box stating that the "Client() contructor has no implementation" or something of the sort....

phew. now i hope that made sense. 

Yey!

Article: Resolving Debugger problems