我有一个内部有许多控件的MDI表单,如按钮,图像,标签,下拉列表等。我已经将表单属性设置为isMDIContainer = true。当我单击表单内的按钮时,将在父级内部打开另一个表单。它的开口,但不幸的是它在所有控件后面的开口。如何使子窗体在主窗体的所有控件前面打开?
Form2 childForm = new Form2();
childForm.MdiParent = this;
childForm.Activate();
childForm.Show();
通常我们不会将任何子控件添加到中Mdi Form
。将aForm
用作an时Mdi Form
,它应包含的唯一子对象是MdiClient
。那MdiClient
将包含您的child forms
。所有控件都应放在上Child forms
。但是,如果您愿意,我们仍然可以使其正常运行
有一个默认MdiClient
包含在Mdi Form
。我们可以在的Controls
集合中找到它Mdi Form
。这是的类型MdiClient
。这将由您的所有其他控件所涵盖Mdi Form
,这就是为什么Child forms
默认情况下无法将您置于首位的原因。为了解决这个问题,只是我们要访问MdiClient
和调用BringToFont()
,每当没有任何Child form
的存在Visible
,我们称之为SendToBack()
上MdiClient
显示你的(按钮,图像,标签,下拉菜单等)的其他控件。这是供您测试的代码:
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句