如何禁用窗体上除按钮以外的所有控件? [英] How to disable all controls on the form except for a button?

查看:134
本文介绍了如何禁用窗体上除按钮以外的所有控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单有数百个控件:菜单,面板,分隔符,标签,文本框,您可以为其命名。

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.

推荐答案

您可以进行递归调用以禁用所有涉及的控件。然后必须启用按钮和所有父容器。

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);
        }
    }

这篇关于如何禁用窗体上除按钮以外的所有控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆