I created a form with many controls. It's a GUI for a measurement program. Therefore some controls must be disabled and some enabled.
When the START button is pressed, every control should be disabled except the ABORT button.
It works with following code:

// Disable each control in the active form's control collection except the ABORT BUTTON 
 for (int i = 0; i < this.Controls.Count; i++)
     this.Controls[i].Enabled = false;
 buttonAbort.Enabled = true;

Well, here's my problem:

AFTER the measurement is done or else the ABORT button is pressed, I want the initial state back.

How can I do this devoid of enable or disable every control on that form?

Thanks for your help.

6 Years
Discussion Span
Last Post by Tortura

Probably the easiest way is to include a boolean field with your objects 'previous state' and set and retrieve previous states from it.


How about using something like:

this.Controls[i].Enabled = !this.Controls[i].Enabled;

To change the state in both ways.


I think you didn't really understand what I meant.
At the beginning, there is not every control enabled. After the measurement is done, I need exactly this state again.

Now I want to know how I can do this without saying for every button, if it's enabled or not.


I tried it with the suggestion of gerard4143.
I created a boolean array and filled it with the status if a control is enabled or not.

Thanks a lot.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.