It’s quite a common problem almost every developer has if he wants to develop a WinForm Application. How to set the control state of buttons, menu items and other WinForm controls?
The stupid approach is to establish a new method that handles the different states of the controls regarding the internal context of the application. This normally ends up in a very big, unconfortable to mantain and extremly buggy SetState Method.
So what is the answer to that problem?
Because I currently have this problem in one of my WinForm projects too, I developed a control extender that manages the states of all WinForm controls transparent for the developer. The advantage is, that it’s highly configurable using the Application Config File.
Here’s an example:
As you can see, every form has it’s own entry within the StateSections. And every control has it’s entry within that section. It’s set up like the WinForm itself.
Now every control has several possibilities to be configured.
| Attribute | Description |
|---|---|
| state | Can be set to enabled / disabled or can be left empty. If a state has been set, all other checks will be ignored. |
| roles | It’s a comma separated list with roles, the current principal must belong to. If the current principal does not belong to the role, the control will be disabled. |
| check | The Control State Extender calls a check routine that tells the extender if the control has to be disabled or enabled. Such a check routine can be implemented in a controller class or within the WinForm itself. |
All you have to do is, throw the ControlStateExtender onto your WinForm and name the controls with a new Property called ControlKey. This ControlKey property is used to identify the control within the App.Config file.
Furthermore the ControlStateExtender control has a property called Section, which defines the section the ControlStateExtender has to use within the config file.
When you initialize your Form you have to initialize the ControlExtenderClass too. The parameter you have to specify is the controller class that handles the check methods. This might be the WinForm itself.
public Form1()
{
InitializeComponent();
controlState.Initialize(this);
}
So the only thing that is left, is to implement the check methods we defined within the application config. This example has a very poor logic, but ok – it’s only for testing purpose.
public ControlState GetButton1State(Control toCheck)
{
return switchButton ? ControlState.Enabled : ControlState.Disabled;
}
Last but not least, you have to initiate the Refresh every time the user press a button. This can’t be done automatically, you have to call the Refresh Method.
private void button1_Click(object sender, EventArgs e)
{
switchButton = false;
controlState.Refresh();
}
Now – watch the application.
The complete example and control state extender can be downloaded using the following link:
Control State Extender and Example Application
I hope that I could make your life a little bit easier with that piece of code.
Wish you all the best.
Cheers
Gerhard