首页 > 代码库 > C#动态添加控件到窗体不显示

C#动态添加控件到窗体不显示

在FormLoad里面添加控件时,用this.Controls.Add()方法向窗体里面动态添加控件,调试,一直不显示,后来发现原来窗体上有一个groupbox覆盖了整个窗体,其实控件已经添加上去了,只是被遮盖了。

后面改为this.groupBox1.Controls.Add(),把控件添加到groupbox里面就不会被遮盖了

 1 private void UniqueForm_Load(object sender, EventArgs e) 2         { 3             if (pFlag == 1) 4             { 5                 Label lbllevel = new Label(); 6                 lbllevel.Text = "请设置级数:"; 7                 lbllevel.Location = new System.Drawing.Point(38, 95); 8                 lbllevel.AutoSize = true; 9                 lbllevel.Visible = true;10                 txtLevel = new TextBox();11                 txtLevel.Name = "txtLevel";12                 txtLevel.Location = new System.Drawing.Point(121, 92);13                 txtLevel.Height = 21;14                 txtLevel.Width = 150;15                 txtLevel.Visible = true;16                 this.groupBox1.Controls.Add(lbllevel);17                 this.groupBox1.Controls.Add(txtLevel);18             }19 }
View Code