Windows Forms, enable disable controls depending on

Does anyone know the right approach to getting controls in an allowed / forbidden form depending on the context? The problem is that I have about 50 controls in my form, and their status should depend on the elements selected in some lists, checked flags and the presence of any data in the database.

A rather crude example is when you have a multi-screen tree view and a button like Compare. This button should be enabled only when the nodes you have selected in the tree structure have the same appearance. In addition, an additional requirement is required, this should be included only if you have selected 2 nodes. And one more, not every class can be compared, so there is a limitation of what exactly you should choose in order to make a possible comparison.

The question is not Control.Enabled = true / false. The question is the general approach.

Thanks.

+4
source share
4 answers

There was something in WPF called the visual state manager. You can implement something similar in WinForms. Basically, your form can have state, and management changes should only be allowed in states. Then you invoke the transition to change state. At least all modification codes can be discarded in methods that are 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 kind of designer support. In WinForms, you could see something like how localization in forms works, but this is due to this.

I made a crude state manager for one project, which took a list of state names of strings and corresponding delegates to the calling methods, then it’s just good practice to save all the modification code in the methods and not decorate the form.

+1
source

Not sure how useful this is for you, but in my case, when so many controls need to be taken for a limited / limited set of conditions, my approaches are:

  • Disable all default controls and enable them only when the conditions necessary for its use are met. This ensures that the user receives a visual signal about when he can use the control, but requires more work, as it requires us to check when conditions are lost, so that the control can be turned off again.

  • All controls are enabled / accessible by default, but only take action / respond to user action only if conditions are required.

But, as you might say, these approaches may not generalize well, and you may need to implement some kind of template / interface that tracks actions / context on behalf of the controls, and then turns them on / off under the right conditions.

+1
source

Another idea might be: each control that requires state management implements the AsbtractControl class, which implies that each child element defines its specific notification state .

Add an EventManager to your application that "intercepts" an event received from different controls other than AsbtractControl and click StateStack . by means of a control only "activated" by the user.

Deploy DesitionalEngine , which parses every StateStack action and enables / disables controls in the user interface, as well as the Clears / or Not stack, based on the cancellation made. The reason, in your opinion, can be accomplished with the permission Enable / Disable, based on the actions of several controls.

Just to give you another idea, call somethign similiar, which I implemented many years ago for WinForms.

Hope this helps.

Sincerely.

+1
source

I have one solution to solve the above problem, if we use the object model, then it will be easy to maintain the state of all controls.

0
source

All Articles