天天看點

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# 周遊界面指定控件