child mdi出现在父控件下方 [英] child mdi appear below the parent control

查看:76
本文介绍了child mdi出现在父控件下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个forms.form1和form2,在form1中有一个menuStrip和一个按钮..当我单击按钮时,form2出现但在form1按钮下面。

我的button1_click的代码是这里:



i have 2 forms.form1 and form2,in form1 there was a menuStrip and a button..when i click the button the form2 appear but below the form1 button.
my code for button1_click is here:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.MdiParent = this;
           
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.WindowState = FormWindowState.Maximized;
          
            frm.Show();
        }

推荐答案

首先,您的代码是C#而不是VB.Net。所以下面的代码解决方案也在C#中。



我想最重要的问题是,你真的想用第二种形式做什么?第二个表单是否显示用户回答问题的对话框?还是显示数据?你真的需要使用MDI吗?从你的问题中不清楚你想要做什么。因此,我将尝试涵盖尽可能多的场景。



查看Word,Excel和其他MDI应用程序的工作原理。主窗体或MDI父级上的所有控件都位于菜单条或工具条中。所以表单的中间部分或MDI容器根本不包含任何控件。



你面临的问题是因为这就是MDI亲子形式的形式工作。这就是为什么你的第二个表单出现在按钮后面的原因。你无能为力。在第二个表单上更改 Z-order 或使用 TopMost 属性不会改变任何内容。



现在,如果您只希望Form2出现在整个应用程序的顶部而不一定受MDI父级的约束,那么以下代码将起作用:



First of all, your code is in C# and not VB.Net. So the code solution below is in C# as well.

I guess the big question is, what are you really trying to do with the second form? Does the second form display a dialog for the user to answer questions? Or display data? Do you really need to use MDI? It's not clear from your question what you are trying to do. So, I will attempt to cover as many scenarios as possible.

Look at how Word, Excel and other MDI applications work. All the controls on the main form, or MDI parent is located either in the menu strip or in a tool strip. So the middle of the form, or MDI container, does not contain any controls at all.

The problem you are facing is because that is how MDI parent-child forms work. And that is why your second form appears behind your button. There is nothing you can do about that. Changing the Z-order or playing around with the TopMost property on the second form will not change anything.

Now, if you just want Form2 to appear on top of the whole application and not necessarily bound by the MDI parent, then the following code will work:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Owner = this;

            frm2.StartPosition = FormStartPosition.CenterParent;

            frm2.TopMost = true;

            frm2.Show();
        }





有两件事需要考虑:



1)定位第二个表格时。考虑一下您希望找到表单的位置。上面的代码将表单放在父表单或调用表单的中心。该行



There are two things to think about:

1) When positioning the second form. Think about where you'd like the form to be located. The code above will position the form in the center of the parent or calling form. The line

frm2.Owner = this;



很重要,以确定所有者是谁,以便Form2知道在哪里定位。



2)你想要第二种形式是模态的吗? 。


is important to establish who the owner is so that Form2 will know where to position itself.

2) Do you want the second form to be modal or not.

frm2.Show();



只需打开第二个表单,即可让用户与第一个表单进行交互。如果你使用


just opens the second form and will allow the user to interact with the first form. If you use

frm2.ShowDialog();



代替,然后第二个表格将成为模态,用户需要关闭它才能与第一个表格进行交互。



如果您仍坚持使用MDI表格,则需要将按钮移动到工具条上,使其位于MDI容器之外。然后你的代码就可以了。


instead, then the second form will become modal and the user will need to close it before they can interact with the first form.

If you still insist on using a MDI form, then you will need to move your button to a tool strip so that it is outside of the MDI container. Then your code will work.


如果你最大化它,为什么要设置表单的StartPosition?这使得StartPosition变得毫无意义。



下一个问题是为什么你甚至使用MDI?
Why are you setting the StartPosition of the form if you're maximizing it? This renders the StartPosition meaningless.

The next question is why are you even using MDI??


这篇关于child mdi出现在父控件下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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