简体   繁体   English

如何将一个窗体添加到另一个窗体winform项目

[英]How to add one form into another form winform project

i tried to create a form instance with in another form and then add that form into main form. 我试图用另一个表单创建一个表单实例,然后将该表单添加到主表单中。 but the form which i added that is not showing. 但是我添加的表格没有显示。 i want to show that form at center at top of all controls. 我想将该表格显示在所有控件顶部的中央。

here is my code 这是我的代码

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);
}

please guide me how to show that form on top of all control of another form at center. 请指导我如何在中心另一种表单的所有控件之上显示该表单。 thanks 谢谢

If I understand your comment correct, your problem is that a DataGrid overlays your recently added form? 如果我理解您的评论正确,那么您的问题是DataGrid覆盖了您最近添加的表单吗? Try : 尝试:

After you have add 添加后

frmContainer.Show();

your Form shoul be visible. 您的表格应该是可见的。 After that you should solve your problem, if you call ucExecludeSpecialist.BringToFront(); 此后,如果您调用ucExecludeSpecialist.BringToFront();应该解决您的问题ucExecludeSpecialist.BringToFront(); after calling frmContainer.Show(); 调用 frmContainer.Show();

Example : 范例:

    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM