天天看点

C# 遍历界面指定控件

  查找界面上所有的TextBox控件,并给他赋初始值为 "aaa" 

public void SetText(Control cc)
        {
            foreach (System.Windows.Forms.Control control in cc.Controls)
            {
                if (control is System.Windows.Forms.TextBox)
                {
                    System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control;
                    tb.Text = "aaa";
                }
                if (control.Controls.Count >0)
                {
                    SetText(control);
                }

            }
        }      

调用方法:

private void Form2_Load(object sender, EventArgs e)
        {
            SetText(this);
        }      

效果图:

C# 遍历界面指定控件