竟然可以这样访问私有控件
建了一个用户控件,名称是UserControl1,上面放一个button,默认的名字是button1,可访问性是privite。
另建一个form窗体,上面放一个Panel,默认名称是panel1,在panel1中加载一个UserControl1实例,竟然可以通过下面的方法对私有控件button1的属性赋值。
private void Form1_Load(object sender, EventArgs e)
{
UserControl1 uc1 = new UserControl1();
panel1.Controls.Add(uc1);
GetFPTitle(this.Controls);
}
public static void GetFPTitle(Control.ControlCollection cc)
{
foreach (Control ct in cc)
{
if (ct is GroupBox)
{
GroupBox gb = (GroupBox)ct;
GetFPTitle(gb.Controls);
}
if (ct is Panel)
{
Panel p = (Panel)ct;
GetFPTitle(p.Controls);
}
if (ct is UserControl)
{
UserControl uc = (UserControl)ct;
GetFPTitle(uc.Controls);