-1

Essentially I have a MDI Parent window, which runs my MainMenu form as a MDI Child, from the Main Menu I can go open other forms. I had the MainMenu then resize the MDI Parent to the size of the MDI Child form. This works perfectly, however the problem is when I go back to the MainMenu form, for some reason the size property has changed to whichever form I had opened.

    private void MDI_MdiChildActivate(object sender, EventArgs e)
    {
        try
        {
            this.ClientSize = this.ActiveMdiChild.Size;
            this.CenterToScreen();
        }
        catch { }
    }

This is the code for resizing my MDI Parent.

I am not asking about how to get the size of a Parent or child form, my code works just fine one way. The issue is that the MainMenu size property changes during run time despite me never changing the size of the MainMenu. The MainMenu form is not the MDI Parent but a child also.

Mcsorley78
  • 11
  • 1
  • 7
  • I must ask, why would you do this? Doing this makes it seem like MDI is not what you should be using. – TyCobb Nov 13 '17 at 20:30
  • @TyCobb it's not conventional I know, however it is for school meaning I am limited to what I can use, and it's mostly just a personal preference to the aesthetics – Mcsorley78 Nov 13 '17 at 20:32

1 Answers1

0

To make a Window as big as your Mdi parent, you should set the FormState of the Mdi Child to maximized instead.

Also, why do you modify the size when an MDI child is activated ? Why not just setting it to Maximized on creation of the MDI child, like this:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();

    f.MdiParent = this;
    f.WindowState = FormWindowState.Maximized;

    f.Show();                
}

If you want to have only one MDI child visible at a time, then make sure you hide the others. You can do it like this for instance:

private Form2 _f2;
private Form3 _f3;

private void open1ToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (_f2 == null)
    {
        _f2 = new Form2();
    }

    ShowForm(_f2);
}

private void open2ToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (_f3 == null)
    {
        _f3 = new Form3();
    }
    ShowForm(_f3);
}

private void ShowForm(Form f)
{

    var otherWindows = this.MdiChildren.Where(child => child != f);

    foreach (var o in otherWindows)
    {
        o.Hide();
    }

    f.MdiParent = this;
    f.WindowState = FormWindowState.Maximized;

    f.Show();
}

Note that you can also choose to close the other forms, depending on what's on them and if you need to save, etc...

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • I'm changing the parent size to the child size. It works fine one way, but going back seems to be the error, I can't seem to figure why my MainMenu size property is changing during runtime. – Mcsorley78 Nov 13 '17 at 20:28
  • It is for aesthetic reasons, I only have one child open at a time. – Mcsorley78 Nov 13 '17 at 20:38
  • I do only have one child open at a time, that isn't the issue. The MainMenu form size property changes during run time despite no code changing the MainMenu form size, the only forms size i change is the MDI parent. – Mcsorley78 Nov 13 '17 at 20:52
  • It is confusing indeed. Set the WindowState to Maximized and it should always remain maximized, even if you resize the mdi-parent. – Frederik Gheysels Nov 13 '17 at 20:54
  • @LarsTech no, as I said a couple of times in my question, I have a form which is my MDI Parent, this form runs my MainMenu as an MDIChild – Mcsorley78 Nov 13 '17 at 20:56
  • So, your menu is an mdi-child. You only have one child open at a time. When you open a form from your menu, how do you go back to your menu ? – Frederik Gheysels Nov 13 '17 at 20:58
  • @FrederikGheysels maximised is not the problem, oddly enough changing it to maximised breaks it all together but that's an issue for another time. – Mcsorley78 Nov 13 '17 at 20:59
  • @FrederikGheysels When I open another form, i store the previous opened form in an array, which i then use to show the previous form. So I just use frm.show(); frm being MainMenu – Mcsorley78 Nov 13 '17 at 21:00
  • @LarsTech all my child forms are borderless – Mcsorley78 Nov 13 '17 at 21:01
  • @Mcsorley78 MDI doesn't properly support that. Get rid of the MDI and just put your forms as TopLevel = false and add it to the parent form in a panel. – LarsTech Nov 13 '17 at 21:02
  • @LarsTech I need all my code to be in separate forms, since it is for school markers insist we must have our code in separate forms – Mcsorley78 Nov 13 '17 at 21:04
  • @Mcsorley78 My comment doesn't change that. – LarsTech Nov 13 '17 at 21:04