如何检索父控件及其子控件 - Winforms C# 2
我想检索表单的控件。要做到这一点:
internal static IEnumerable<Control> EnumereTousControle(Control controleParent)
{
foreach (Control subControl in controleParent.Controls)
{
yield return subControl;
foreach (Control c in EnumereTousControle(subControl))
yield return c;
}
}
它工作正常,父控件的所有子控件都被检索。但我也需要这个方法返回父控件。我尝试了这个,但它不起作用(因为该方法是递归的):
internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
yield return parentControl; // does not work
foreach (Control subControl in parentControl.Controls)
{
yield return subControl;
foreach (Control c in EnumereAllControls(subControl))
yield return c;
}
}
感谢您的帮助!
I want to retrieve controls of a form. To do this :
internal static IEnumerable<Control> EnumereTousControle(Control controleParent)
{
foreach (Control subControl in controleParent.Controls)
{
yield return subControl;
foreach (Control c in EnumereTousControle(subControl))
yield return c;
}
}
It works fine, all children of the parent control are retrieved. But I need this method returns the parent control too. I tried this but it don't works (because the method is recursive) :
internal static IEnumerable<Control> EnumereAllControls(Control parentControl)
{
yield return parentControl; // does not work
foreach (Control subControl in parentControl.Controls)
{
yield return subControl;
foreach (Control c in EnumereAllControls(subControl))
yield return c;
}
}
Thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
想想你的一行太多了,试试这个:
Thnk you just had one line too many, try this:
好吧,同样的事情减去你得到的额外收益。
Well, same thing minus the extra yield you got.
如果这不是约束,您可以将父控件添加到方法外部的结果中。
You could add the parent control to the result from outside the method if that is not a constraint.