关闭子mdi c#使用父mdi上的主菜单条控件

时间:2013-09-25 04:54:12

标签: c# mdi

我正在使用C#形式在Visual Studio 2010中创建一个Windows窗体应用程序。我已经设法将子表单连接到父mdi表单上相应的菜单条控件,但是,我希望将这些子mdi表单停靠到父mdi的大小,并在使用另一个子mdi表单打开时自动关闭它们另一个菜单条控件。

例如,我有一个名为Tile Model的菜单条项,它在单击时调用/打开一个特定的子表单。当我点击另一个菜单条项时,比方说,称为帐户,由Tile Model菜单项调用的子表单必须自动关闭,并且Accounts菜单条项目调用的子表单将打开。

请注意,我已将表单边框样式设置为“none”。

我现在所遇到的代码是,每当打开一个子mdi表单并且它处于活动状态时,会打开另一个子表单,这些chilf表单只是重叠而且看起来很混乱。

这里摘自我的代码。

公共部分类Form1:表单     {         公共Form1()         {             的InitializeComponent();         }

    private void manageTileModelToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ManageTileModel ChildForm = new ManageTileModel();
        ChildForm.MdiParent = this;
        ChildForm.Show();

    }

    private void startInspectionToolStripMenuItem_Click(object sender, EventArgs e)
    {
        StartInspection ChildForm = new StartInspection();
        ChildForm.MdiParent = this;
        ChildForm.Show();


    }

    private void manageTestReportsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ManageTestReports ChildForm = new ManageTestReports();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void registerNewAccountToolStripMenuItem_Click(object sender, EventArgs e)
    {
        RegNewAccount ChildForm = new RegNewAccount();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void manageAccountsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ManageAccounts ChildForm = new ManageAccounts();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void inspectionToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        GenHelpInspection ChildForm = new GenHelpInspection();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void tileModelToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        GenHelpTileModel ChildForm = new GenHelpTileModel();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void accountsToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        GenHelpAccounts ChildForm = new GenHelpAccounts();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        About ChildForm = new About();
        ChildForm.MdiParent = this;
        ChildForm.Show();
    }

回复非常有用,我将非常感激。:)谢谢。

1 个答案:

答案 0 :(得分:0)

如果您只想显示单个MDI子项,那么如何使用私有字段存储最后显示的子项。然后你可以写一个像这样的方法:

private void HideLastChild() {
    if(_lastChild != null)
        _lastChild.Close();
}

并在ChildForm.Show()之前调用它,当然您还必须更新_lastChild字段:

ChildForm.Show();
_lastChild = ChildForm;

您可以将所有显示操作组合到单个通用方法中:

private void ShowChild<TWindow>() where TWindow: Form, new() {
    var child = new TWindow();
    HideLastChild();
    _lastChild = child;
    child.MDIParent = this;
    child.Show();
}

ShowChild<About>();