How to disable all controls on the form except for a button?

c#, controls, winforms

Solution

You can do a recursive call to disable all of the controls involved. Then you have to enable your button and any parent containers.

 private void Form1_Load(object sender, EventArgs e) {
        DisableControls(this);
        EnableControls(Button1);
    }

    private void DisableControls(Control con) {
        foreach (Control c in con.Controls) {
            DisableControls(c);
        }
        con.Enabled = false;
    }

    private void EnableControls(Control con) {
        if (con != null) {
            con.Enabled = true;
            EnableControls(con.Parent);
        }
    }

Problem

My form has hundreds of controls: menus, panels, splitters, labels, text boxes, you name it. Is there a way to disable every control except for a single button? The reason why the button is significant is because I can't use a method that disables the window or something because one control still needs to be usable.

Original source

Related problems