views:

39

answers:

1

In a project in C# (.Net 2.0) I use a propertygrid. This propertygrid displays objects retrieved from a PHP backend via SOAP. Some objects contain string properties where the meaning of an empty string is different compared to the meaning of a string that's NULL. An example of an object returned by a SOAP call might be:

SomeObject {
  PropertyA = "Foo"
  PropertyB = "Bar"
  PropertyC = Null
}

As long as I don't "touch" PropertyC in the PropertyGrid, the value of PropertyC will remain null. When I enter "foobar" as value and then clear the text in the propertygrid for PropertyC the value will equal an empty string. This is all fine by me; the user should be able to "enter" an empty string. But I also want the user to be able to specify a "null" value.

The way I envision this is that the user can Right-click the value of the property and a context-menu will pop-up with a "Clear value" option which would set the value to Null.

Important: I do not want to use a "magic value" like the string "Null" or "Magix123" to specify the value needs to be null. Ofcourse, the backend could interpret these values and store an actual null but this "solution" is, apart from being plain dirty, not wanted because it would require changing a lot of code all over the place handling the "Magic values".

Ideally the PropertyGrid would also display a null value as ("grayed") "" so the user can see the difference between an empty string and a Null value.

Anyone have an idea on how to handle this kind of situation? Does the (.Net 2.0!) PropertyGrid allow me to do this kind of stuff? And how would I have to go about it then? Would I need to create my own usercontrol derived from a PropertyGrid or does the PropertyGrid have some function(s) I missed?

+2  A: 

Adding a DefaultValueAttribute with default value null to the property should enable the command Reset in the context menu. For further customization you will probably have to add an EditorAttribute and write a custom UITypeEditor.

Daniel Brückner
You've got to be kidding me. That simple? Jeeeeeez... LOLI'll go check it out and let you know. Thanks!
RobIII
At least in theory it should be that easy. There is a lot of functionality and magic integrated in many controls.
Daniel Brückner
Hmmm, I have added [DefaultValue(null)] to a property in the Reference.cs file but the "reset" command is not available. Maybe I'm doing something wrong here. I'm working on it... any help/tips are appreciated.Also; I'm going to have to figure out how to apply these changes in another place than References.cs because this file will always be overwritten (regenereated) when updating the web-reference. I guess Partial classes are the way to go?
RobIII
Hmmm, might check out [System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
RobIII
You are right - it does not work with the standard `PropertyGrid`. It only works with the property grid of the WinForms designer out of the box because they extended it in this way. But it is quite easy to add this functionality to a custom property grid.
Daniel Brückner
Hmmm, I might be misunderstanding you but I _am_ using a "standard" propertygrid in my project (just dragged in on a form in a winforms project).
RobIII
If you write a custom WinForms control and use and edit if with the WinForms designer the property grid of the WinForms designer will behave as I stated in my answer - that is if you have a property with the `DefaultValue` attribute on a custom WinForms control the designer will show the Reset context menu item. But that functionality is not included in the standard property grid so you will have to implement it yourself. I would have sworn that I used it with the standard property grid, but I checked it and it does not work...
Daniel Brückner
Have a look at this sample code. http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9dd7b9bf-4134-4105-aba8-65002fed04f2 There is also this method. http://msdn.microsoft.com/en-us/library/system.windows.forms.propertygrid.resetselectedproperty.aspx And finally there is the Reset method pattern. http://msdn.microsoft.com/en-us/library/53b8022e(vs.71).aspx
Daniel Brückner
You will have to play a bit around with these and see what works - I cannot tell for sure because some of the stuff will work with the default property grid and some is only build into the property grid of the WinForms designer and will work with custom WinForms controls.
Daniel Brückner