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 nodes 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.