1
votes

I'm creating a windows form application in visual studio 2010 using C# which was in a mdi form. I have already managed to connect child forms to their corresponding menu strip controls on the parent mdi form however, i wish to dock these child mdi forms to the size of the parent mdi as well as close them automatically as another child mdi form is opened using another menu strip control.

for example, i have a menu strip item called Tile Model and it calls/opens a specific child form when clicked. When I clicked on another menu strip item, lets say, called Accounts, the child form called by the Tile Model menu item must automatically close and the child form called by the Accounts menu strip item will open.

Note that i've set the form border style to "none".

what happens to the code i have right now is that whenever a child mdi form is opened and as it is active, another child form is opened, these chilf forms just overlap and it appears to be messy looking.

here an excerpt from my code.

public partial class Form1 : Form { public 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();
    }

A response will very helpful and I will truly appreciate it.:) thanks.

1

1 Answers

0
votes

If you want to display only single MDI child, how about using private field to store last displayed child. Then you can write a method like this:

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

and call it before ChildForm.Show(), of course you must also update _lastChild field:

ChildForm.Show();
_lastChild = ChildForm;

You can combine all displaying operations into single generic method:

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

ShowChild<About>();