使用C#的.NET SplitContainer

时间:2012-09-05 09:40:05

标签: c# .net winforms splitcontainer

我在C#窗口应用程序中使用SplitContainer工具

我想在分割的容器面板中替换其他表单的一个表单

我该怎么做?

我想在From From1工作完成它的工作并显示From2 ..替换拆分容器面板的相同位置......

但是这段代码不起作用......

public partial class Parent : Form{public Parent()
{
    InitializeComponent();
}


private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNode node = treeView1.SelectedNode;

    if (node.Text.ToString().Equals("Control1"))
    {
        //MessageBox.Show(node.ToString());
        //Control1 conr1 = new Control1();
        ShowForm(1);
    }
    else if (node.Text.ToString().Equals("Control2"))
    {
        //MessageBox.Show(node.ToString());
        //Control2 conr2 = new Control2();
        ShowForm(2);
    }

}

public void ShowForm(int id)
{
    Form childObj = null;
    if (id == 1)
    {
        childObj = new Control1();

    }
    else
    {
        childObj = new Control2();
    }
    childObj.TopLevel = false;
    childObj.Visible = true;
    childObj.Parent = this.splitContainer1.Panel2;

    this.splitContainer1.Panel2.Controls.Clear();
    this.splitContainer1.Panel2.Hide();
    this.splitContainer1.Panel2.Controls.Add(childObj);
    this.splitContainer1.Panel2.Show();
    childObj.Show();
}

public Control2()
{
    InitializeComponent();
}

Parent bioAdMainForm = new Parent(); 
private void button1_Click(object sender, EventArgs e)
{
    //Control1 enrollmentForm = new Control1();
    //this.Hide();
    //enrollmentForm.Show();
    bioAdMainForm.ShowForm(1);
}

1 个答案:

答案 0 :(得分:-1)

您无法将Form放入Panel。表单旨在显示在单独的窗口中。您应该使用UserControl后代而不是表单来实现您想要的目标。