如何将一个表单添加到另一个表单winform项目中

时间:2013-01-22 11:26:37

标签: c# winforms

我尝试用另一种形式创建一个表单实例,然后将该表单添加到主表单中。但我添加的表格没有显示。我希望在所有控件的顶部显示该窗体。

这是我的代码

BBA.Controls.ExecludeSpecialist ucExecludeSpecialist = null;
Form frmContainer = null;

private void btnExclude_Click(object sender, EventArgs e)
{
    if (ucExecludeSpecialist != null)
    {
        if (frmContainer != null)
        {
            frmContainer.Controls.Remove(ucExecludeSpecialist);
            ucExecludeSpecialist = null;
        }
    }

    if (frmContainer != null)
    {
        this.Controls.Remove(frmContainer);
        frmContainer = null;
    }

    frmContainer = new Form();
    frmContainer.ControlBox = false;
    frmContainer.StartPosition = FormStartPosition.Manual;
    frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

    ucExecludeSpecialist = new BBA.Controls.ExecludeSpecialist();
    ucExecludeSpecialist.SaveClicked += 
        new BBA.Controls.ExecludeSpecialist.SaveComplete(OnSaveClicked);
    ucExecludeSpecialist.CloseClicked += 
        new BBA.Controls.ExecludeSpecialist.CloseComplete(OnCloseClicked);
    ucExecludeSpecialist.BringToFront();
    frmContainer.Height = ucExecludeSpecialist.Height;
    frmContainer.Width = ucExecludeSpecialist.Width;
    //frmContainer.Top = this.Height - frmContainer.Height / 2;
    //frmContainer.Left = this.Height - frmContainer.Height / 2;
    frmContainer.BringToFront();
    frmContainer.TopLevel = false;
    frmContainer.Controls.Add(ucExecludeSpecialist);
    this.Controls.Add(frmContainer);
}

请指导我如何在中心的另一个表格的所有控制之上显示该表格。感谢

1 个答案:

答案 0 :(得分:1)

如果我理解您的评论正确,那么您的问题是DataGrid会覆盖您最近添加的表单吗?试试:

添加

frmContainer.Show();

你的表格应该可见。如果您在致电ucExecludeSpecialist.BringToFront();之后致电frmContainer.Show();

,那么您应该解决您的问题

示例:

    private void button1_Click(object sender, EventArgs e)
    {

        frmContainer = new Form();
        frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        frmContainer.Height = this.Height / 2;
        frmContainer.Width = this.Width / 2;
        frmContainer.BackColor = Color.Red;
        frmContainer.TopLevel = false;
        this.Controls.Add(frmContainer);
        frmContainer.Show();
        frmContainer.BringToFront();
    }