我有主窗体和2个usercontrols.The主窗体包含拆分容器,在splitcontainer.panel1中我加载了UserControl1。在usercontrol中放置了不同的按钮。我想在按钮点击上加载user2上的usercontrol2(在主窗体中),按钮点击放在usercontrol1中。
public partial class Form1 : Form
{
UserControl1 obj = new UserControl1();
public Form1()
{
InitializeComponent();
splitContainer1.Panel1.Controls.Add(obj);
}
}
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event EventHandler ButtonClick;
public void button1_Click(object sender, EventArgs e)
{
}
}
在button1_Click上我想在表单splitcontainer panel2上加载UserControl2
答案 0 :(得分:1)
您可以像这样更改UserControl1:
public void button1_Click(object sender, EventArgs e)
{
if(ButtonClick != null)
ButtonClick(this, e);
}
然后在Form1构造函数中添加以下代码:
obj.ButtonClick += (Sender, e) =>
{
splitContainer1.Panel2.Controls.Add(obj2);
};
这应该有效