Automatic setting of Controls' background color

Automatic setting of Controls' background color

Old forum URL: forums.lhotka.net/forums/t/5959.aspx


lukky posted on Tuesday, December 09, 2008

Hi,

I've had a request from a client where he'd like to show a different background color on bound controls when the bound to BO is newly created, up until such time when user actually changes the default value for all those colored controls.

I'm thinking that maybe this could be done through an extender control. Maybe I could setup a dictionary of controls that this extender control extends, and then respond to the Validating event to set the color back to normal color.

Is this how you guys would handle it ? I don't think this should go into my BOs, should it ? Or is there already a mechanism in CSLA that would tell me if a managed property has been modified since a certain point in time ?

Any ideas/suggestions/laughs ?

Thank you

JoeFallon1 replied on Tuesday, December 09, 2008

There have discussions about having a Managed property be aware of its initial value so that if it is changed then the property could tell you if it was dirty or not. Then if you changed it back to the original value it would not be dirty! The dirty state of the BO would be a loop over all the managed properties until the first IsDirty is True, else it is False.

Rocky does not want to implement this (yet? / ever?)  because it "doubles" the size of the graph. But that assumes every property would always need to know its original value. Maybe only a subset require this functionality.

At any rate, if you search for this you should find some sample code I posted on how to implement this idea by extending Rocky's classes. Once you have these classes in place, each property can tell you if it is dirty or not and your problem above becomes trivial.

Joe

 

SonOfPirate replied on Wednesday, December 10, 2008

Yes, you could create an extender class that would "default enable" a control.  That would be the best way to encapsulate the logic and allow you to reuse it on your forms without having to add more code.

To accomplish it, I would suggest making use of the existing System.ComponentModel.DefaultValue attribute.  You can mark your BO properties with this attribute and specify the appropriate value.  Then your extender can compare this value to the current property value to determine if it has changed or not.

Alternatively, your extender can have an InitialValue property that is exposed to the UI.  When designing the form, specify the initial/default value here and have the extender modify the extended controls properties according to whether the bound BO's property is or isn't set to the InitialValue.  This would require you to hard-code the value for each extended control and takes the default value definition away from the BO - which is why I prefer the first approach.

Hope that helps.

 

lukky replied on Wednesday, December 10, 2008

I kind of like the Extender Control way of doing this because it allows me to reuse it on other forms.

Speaking with the client, we have agreed (read, I was able to convince him) that the background color of those controls should be set to another color when a new BO is created, those fields having empty initial values, then as soon as the user enters something, the color should revert to normal.

This way, I don't have to worry about initial/default values. It's simply a visual cue that the value was never set.

Though, in some other cases, it might be handy to compare to some initial value, so maybe it could be done by "loading" the Extender Control with a snapshot of the values (after BO creation), then as each one of them validates, if the current value is not equal to the "snapshot" value, revert the color to normal.

This sounds fun Smile [:)]

Copyright (c) Marimer LLC