本文介绍了如何从包含任何容器中的控件的表单中获取所有控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我需要一种方法来禁用表单中的所有按钮或验证所有文本框的数据.有任何想法吗?提前致谢!
I need, for example, a way to disable all buttons in a form or validate all textboxes' 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);
}
}
然后是:
ApplyAll(this, c => c.Validate());
ApplyAll(this, c => {c.Enabled = false; });
这篇关于如何从包含任何容器中的控件的表单中获取所有控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!