【发布时间】:2013-03-03 14:43:44
【问题描述】:
我想为我的Form 上的所有文本框添加一个事件:
foreach (Control C in this.Controls)
{
if (C.GetType() == typeof(System.Windows.Forms.TextBox))
{
C.TextChanged += new EventHandler(C_TextChanged);
}
}
问题是它们存储在几个 GroupBox 中,而我的循环看不到它们。我可以单独循环遍历每个 GroupBox 的控件,但是否可以在一个循环中以简单的方式完成所有操作?
【问题讨论】:
-
你可以在递归循环中完成。
-
我为此做了一个API Proposal: Add Descendants property for Control。如果你喜欢它,请在 github.com/dotnet/winforms 上投票。