Does anybody know the right approach to getting the controls on the form enabled/disabled depending on context? The problem is, I have about 50 controls on my form and their states should depend on items selected in some lists, checkboxes checked and some data on the DB exists or not.
Quite rough example is when you have a multi-select tree view and a button like "Compare". This button should only be enabled when the nod开发者_JAVA百科es you've selected on the tree view are all of the same kind. Moreover, an additional requirement is, this should be enabled only if you selected 2 nodes. And one more, not every class can be compared, so there's also a limitation to what exactly you should select to make the comparison possible.
The question is not about Control.Enabled = true/false. The question is about generic approach.
Thanks.
Well in WPF there was something called the Visual State Manager. You could implement something similar in WinForms. Basically your form could have state, and control modifications should only be allowed in states. Then you call a transition to change state. At least all the modification code could then be squirrelled away in methods somewhere called by the state manager.
http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-visual-state-manager-overview.aspx
In WPF I think there is some sort of designer support. It would be possible in WinForms following something like how localisation works on forms, but it's involved.
I did a crude state manager for one project that took a list of string state names and corresponding delegates to methods to call, then it's just good practice to keep all modification code in methods and not decorated about the form.
Am not sure how helpful this might be to you, but in my case, when so many controls need to take action on a limited / constrained set of conditions, then my approaches are:
Disable all controls by default, and only enable any, when the conditions required for it's use are met. This ensures that the user gets a visual cue of when they can use a control, but takes more work, since it requires that we check when the conditions are lost such that the control can be disabled again.
Have all controls enabled / available by default, but only take action / respond to user action only if the required conditions are met.
But like you might tell, these approaches might not generalize well, and you might need to implement some sort of pattern / interface that observes actions / context on behalf of the controls, and then turns them on/off when the right conditions are met.
Another idea could be: is to make every control that need state management implement AsbtractControl class which implies on every child define it's specific notification state.
Implement EventManager in your app that "hooks" event recieved from different controls, cast them like AsbtractControl and push on StateStack the state implied by the control just "activated" by user.
Implement DesitionalEngine that parse on every action StateStack and Enables/Disables controls on UI and also Clears/Or Not stack based on desition made. Cause, as you said, the desition Enable/Disable can be done also base on multiple controls actions.
Just to give you another idea, cause somethign similiar I implemented years ago for WinForms.
Hope this helps.
Regards.
I have one solution to over come the above mentioned problem if we are using object model then it will be easy to maintain the state of all controls.
精彩评论