通常我们不会将任何子控件添加到Mdi Form
. 当 aForm
用作 a时Mdi Form
,它应该包含的唯一子级是MdiClient
。这MdiClient
将包含您的child forms
. 所有控件都应放在Child forms
. 但是,如果您愿意,我们仍然可以使其工作
MdiClient
a中包含一个默认值Mdi Form
。我们可以在Controls
集合中找到它Mdi Form
。它的类型MdiClient
。这将被您的所有其他控件所覆盖,Mdi Form
这就是为什么Child forms
默认情况下不能将您置于首位的原因。为了解决这个问题,我们只需访问MdiClient
and 调用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);
}
}