子窗体在c#中主窗体的控件后面打开 [英] Child form opened behind the controls of the main form in c#

查看:51
本文介绍了子窗体在c#中主窗体的控件后面打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MDI 表单,里面有很多控件,比如按钮、图像、标签、下拉等.我已经设置了表单属性 isMDIContainer=true.当我点击表单内的一个按钮时,另一个表单将在里面打开parent.现在打开了,可惜在所有控件后面都打开了.如何让子窗体在主窗体的所有控件前面打开?

I have a MDI form with many controls inside it like button,images,label,dropdown etc.I have made the form property isMDIContainer=true.When i click on a button inside the form another form is to be opened inside the parent.Now its opening,but unfortunately its opening behind all the controls.How to make the child form open infront of all the controls of main form?

    Form2 childForm = new Form2();            
    childForm.MdiParent = this;
    childForm.Activate();          
    childForm.Show();

推荐答案

通常我们Mdi Form添加任何子控件.当 Form 用作 Mdi Form 时,它应该包含的唯一子项是 MdiClient.MdiClient 将包含您的 子表单.所有的控件都应该放在子窗体上.但是,如果您愿意,我们仍然可以使其正常运行

Normally we don't add any child controls to a Mdi Form. When a Form is used as an Mdi Form, the only child it should contain is MdiClient. That MdiClient will contain your child forms. All the controls should be placed on the Child forms. However if you want so, we can still make it work

Mdi 表单 中包含一个默认的MdiClient.我们可以在 Mdi FormControls 集合中找到它.它是 MdiClient 的类型.这将由您的 Mdi 表单 的所有其他控件覆盖,这就是默认情况下您的 子表单 不能置于顶部的原因.要解决它,我们只需访问 MdiClient 并调用 BringToFont() 并且只要没有任何 Child form 成为 >Visible,我们将在 MdiClient 上调用 SendToBack() 来显示您的其他控件(按钮、图像、标签、下拉菜单等).以下是供您测试的代码:

There is a default MdiClient contained in a Mdi Form. We can find it in the Controls collection of the Mdi Form. It's type of MdiClient. This will be covered by all other controls of your Mdi Form and that's why your Child forms can't be brought on top by default. To solve it, simply we have to access to the MdiClient and call BringToFont() and whenever there isn't any Child form being Visible, we will call SendToBack() on the MdiClient to show the other controls of yours (button,images,label,dropdown etc). Here is the code for you to test:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        IsMdiContainer = true;
        //Find the MdiClient and hold it by a variable
        client = Controls.OfType<MdiClient>().First();
        //This will check whenever client gets focused and there aren't any
        //child forms opened, Send the client to back so that the other controls can be shown back.
        client.GotFocus += (s, e) => {
            if (!MdiChildren.Any(x => x.Visible)) client.SendToBack();
        };
    }
    MdiClient client;
    //This is used to show a child form
    //Note that we have to call client.BringToFront();
    private void ShowForm(Form childForm)
    {
        client.BringToFront();//This will make your child form shown on top.
        childForm.Show();            
    }
    //button1 is a button on your Form1 (Mdi container)
    //clicking it to show a child form and see it in action
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2 { MdiParent = this };
        ShowForm(f);         
    }     
}

这篇关于子窗体在c#中主窗体的控件后面打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆