如何使窗体上的所有控件? [英] How to enable ALL controls on a form?

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

问题描述

目前我最有推出禁用我的窗体的控件,因为直到文件被加载,你不能使用它们。然而,一旦文件被加载控件应该会启用。

Currently I have most of my form's controls disabled at launch because you cannot use them until a file is loaded. However, once the file is loaded the controls should become enabled.

我是使用绑定,但我不认为他们是一个很好的解决方案。首先,它是不必要的复杂性。其次,您不能使用绑定的一切。例如,MenuStrip中的项目不能有自己的Enabled属性绑定到fileLoaded财产。只有整个菜单可以,我不希望禁用在启动整个菜单,只有对文件进行操作的某些菜单操作。

I was using bindings but I don't think they're a good solution. For one, it is unnecessary complexity. Secondly, you cannot use bindings for everything. For example, MenuStrip items cannot have their Enabled property bound to the fileLoaded property. Only the entire menu can and I don't want to disable the entire menu at launch, only certain menu operations that operate on the file.

我真的只是寻找一种方式,以使一切。当被问及最,将有这样的回答:

I'm really just looking for a way to enable EVERYTHING. Most when asked that would answer with this:

foreach (Control c in Controls)
{
    c.Enabled = true;
}



然而,这并不用于使其他控件内的MenuStrip项或控制工作(如面板或自定义控制)。因此它不会使容器内的滚动条。

However, that does not work for enabling MenuStrip items or controls within other controls (like a Panel or custom control). Therefore it would not enable scrollbars within containers.

我想我可以使用该行并手动启用一切,但我可​​以永远只是手动的启用一切。我正在寻找一种方法来自动启用的所有

I suppose I could use that line and manually enable everything else but I could have always just manually enabled everything. I'm looking for a way to automatically enable everything.

推荐答案

递归

private void enableControls(Control.ControlCollection Controls)
{
    foreach (Control c in Controls)
    {
        c.Enabled = true;
        if (c is MenuStrip)
        {
           foreach(var item in ((MenuStrip)c).Items)
           { 
              item.Enabled = true;
           }
        }
        if (c.ControlCollection.Count > 0)
            enableControls(c.Controls);

    }
}

修改

本来应该检查控件集合数,而不是HasControls哪家器WebControls

Should have been checking the control Collection count instead of HasControls Which is Webcontrols

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

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