我想使用用户控件在一个表单中加载多个表单,我尝试了以下代码,但在单击 button1 后似乎没有任何反应。任何人都知道怎么了?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 control = new UserControl1();
control.Dock = DockStyle.Fill;
this.Controls.Add(control);
}
}
最佳答案
however the contents of UserControl1 seems to be overlapping and I can still see the content of Form1
窗体上控件的 Z 顺序很重要。使用 Controls.Add(),控件最终位于顺序的底部,现有控件与它重叠。你这样修复它:
this.Controls.Add(control);
control.BringToFront();
或使用 Controls.SetChildIndex() 将其插入控件之间。
关于c# - 使用多种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140926/