简体   繁体   English

如何在ac#winform应用程序中将子窗口置于前面?

[英]How to bring a child window to the front in a c# winform application?

When I click on a button in the main form, I want another form to open within the main form. 当我单击主窗体中的按钮时,我希望在主窗体中打开另一个窗体。 This works so far with the following code: 到目前为止,这使用以下代码:

private void btnOpenChildForm(object sender, EventArgs e)
{       
    ChildForm Form = new ChildForm();
    this.IsMdiContainer = true;
    Form.MdiParent = this;

    Form.Show();
}

Problem: 问题:

The buttons and other controls of the main form are still visible in the child form. 子表单中的按钮和其他控件仍然可见。 I tried it with Form.BringToFront() , but it did not work neither. 我尝试使用Form.BringToFront() ,但它既不起作用。

Update 更新

This is what worked so far for me, after deciding to change the GUI design. 在决定更改GUI设计之后,这是我迄今为止所做的工作。 I took a ToolStripMenuItem instead of the buttons I had before. 我拿了一个ToolStripMenuItem而不是之前的按钮。

A global variable(I think I have to improve that) 一个全局变量(我想我必须改进它)

    ChildForm frmChildForm;

The click method: 点击方法:

    private void frmChildFormToolStripMenuItem_Click(object sender, EventArgs e)
            {

                if (frmChildForm  == null || frmChildForm .IsDisposed == true)
                {
                    frmChildForm  = new ChildForm();

                }

                this.IsMdiContainer = true;
                frmChildForm.MdiParent = this;
                frmChildForm.WindowState = FormWindowState.Maximized;
                frmChildForm.Show();

            }   

I Think , you need to use Focus() method for that. 我想,你需要使用Focus()方法。

private void btnOpenChildForm(object sender, EventArgs e)
{
    ChildForm Form = new ChildFrom();
    this.IsMdiContainer = true;
    Form.MdiParent = this;
    Form.Show();
    Form.focus();
}

Like i said Ideally the MDI should only have the Menu control and Status Bar and that would be the intention of a MDI. 就像我说的理想情况下,MDI应该只有菜单控件和状态栏,这将是MDI的意图。 To just act as a container for other forms nothing else. 只是作为其他形式的容器,没有别的。

So if you just want a child form to open up on top of your parentform do this (SDI way). 因此,如果您只想在父窗体上打开子窗体,请执行此操作(SDI方式)。

private void btnOpenChildForm(object sender, EventArgs e)
{   
    //if you just want the form to show on top do not make the MDI

    ChildForm Form = new ChildForm();
    Form.Show();
    Form.Owner=this;    
}

Can you try this from http://www.codeproject.com/Articles/7571/Creating-MDI-application-using-C-Walkthrough : 你可以试试http://www.codeproject.com/Articles/7571/Creating-MDI-application-using-C-Walkthrough

ChildForm Form = ChildForm.GetChildInstance();
Form.MdiParent = this;
Form.Show();
Form.BringToFront();

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

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