我怎样才能从形式,包括任何容器中控制所有的控制? [英] How can I get all controls from a Form Including controls in any container?

查看:121
本文介绍了我怎样才能从形式,包括任何容器中控制所有的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要例如禁用所有按钮在窗体或验证所有的文本框的数据...任何想法..在此先感谢!

I need for example to disable all buttons in a form or validate all textbox's data... any Ideas.. thanks in advance!!!

推荐答案

最简单的办法可能是级联的:

The simplest option may be to cascade:

public static void SetEnabled(Control control, bool enabled) {
    control.Enabled = enabled;
    foreach(Control child in control.Controls) {
        SetEnabled(child, enabled);
    }
}

或类似的;你当然可以通过委托,使其非常通用的:

or similar; you could of course pass a delegate to make it fairly generic:

public static void ApplyAll(Control control, Action<Control> action) {
    action(control);
    foreach(Control child in control.Controls) {
        ApplyAll(child, action);
    }
}



那么这样的话:

then things like:

ApplyAll(this, c => c.Validate());

ApplyAll(this, c => {c.Enabled = false; });

这篇关于我怎样才能从形式,包括任何容器中控制所有的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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