繁体   English   中英

如何获取Windows窗体页面上的所有控件

[英]how to get all the controls on the windows form page

我有一个窗户表格; 并且有几个控件。
我想让它们处于foreach循环中,以调用每个控件的Clear()方法,以使其清晰并重新初始化。

我该怎么做?**

当我在Vs 2008的调试模式下浏览表单页面时,看到“ this”,因此可以看到其中的所有内容。

.net版本:2.0

您可能在控件等控件上拥有控件。因此,将Dmitry Erokhin的代码放在递归函数中可能是一个好主意:

private void ClearNumberEntries(ControlCollection controls)
{
    foreach (Control ctrl in controls)
    {
        if (ctrl is NumberEntry)
        {
            ((NumberEntry)ctrl).Clear();
        }
        //if you are sure a NumberEntry can never have child controls that could also be of type NumberEntry you can put this in an else in stead
        ClearNumberEntries(ctrl.Controls);
    }
}

您可以通过以下控件进行迭代:

foreach (Control ctrl in this.Controls)
{
    if (ctrl is NumberEntry)
    {
        ((NumberEntry)ctrl).Clear();
    }
}

再次初始化控件无需清除每个控件,只需清除表单中的控件并调用InitializeComponent()

 private void InitializeControls()
        {
            this.Controls.Clear();
            InitializeComponent();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM