0

I'm using C# Windows Forms. I have an MDI app with a panel anchored to the left side of the MDI form. I'm opening MDI child windows maximized, which works fine. However, I two two separate problems, which are probably linked, which are:

When I click the restore button on the MDI child form, the form goes to a sort of pseudo-minimized state i the middle of the MDI parent. It's not possible to resize it and stretch it out, or even move it. I can then minimize it (to the bottom of the parent), restore it (to the same strange pseudo-minimized state) or maximize it as before. How to I get it to go to it's normal state... errr... normally.

Another effect which is occuring, which may or may not be related is that when I open the child, the form's title bar doesn't integrate with the MDI menu. There is the child window bar and then under that is the MDI menu bar. Normally these integrate.

I've created a short video to demonstrate this, a picture is worth a thousand words, here: https://youtu.be/l1yFeDFJeMQ

For clarity, the name of the MDI parent for is "mdi". The name of the MDI child form is "frmForm". This might sound strange, but the purpose of "frmForm" is to enable you to design forms.

The calling code in the MDI parent form is this:

private bool EditForm(AppData.Server oServer, AppData.App oApp, int iFrmID)
{   
    // this edits the form passed within the indicated Appster server/app.
    frmForm oFrmForm;

    try
    {
        oFrmForm = new frmForm();
        if (!oFrmForm.EditForm(_owner, _g, oServer, oApp, iFrmID)) throw new Exception("Cannot show form for editing.");

        return true;
    }
    catch (Exception ex)
    {
        _g.Errs.Raise(ex);
        return false;
    }
}

The code in the MDI child form is here:

public bool EditForm(Form oMDIParent, AppData.Globals oG, AppData.Server oServer, AppData.App oApp, int iFrmID)
{
    try
    {
        _g = oG;
        _app = oApp;
        _server = oServer;
        _mode = Dolphin.enumFormEditMode.Edit;

        toolbox.Init(_g, (TextBox)fd.Controls["txtDebug"]);
        fd.Init(_g, _server);
        fd.SnapToGrid = true;
        
        svEvents.Init(_g);
        svSteps.Init(_g);

        if (!ReadData(iFrmID)) throw new Exception("Could not read form data.");
        if (!PopulateParams()) throw new Exception("Could not populate parameters list correctly.");
        if (!PopulateQueries()) throw new Exception("Could not populate queries list correctly.");
        if (!PopulateModes()) throw new Exception("Could not populate modes list correctly.");
        if (!PopulateControls()) throw new Exception("Could not populate controls list correctly.");
        if (!PopulatePropControls(false)) throw new Exception("Could not populate properties control dropdown correctly.");
        if (!PopulatePropMode(false)) throw new Exception("Could not populate properties mode dropdown correctly.");
        if (!ShowData()) throw new Exception("Could not populate with form data.");
        //if (!SetControlAccess()) throw new Exception("Could not set access to controls correctly.");
        _isDirty = false; // has to happen after all the fields are setup
        if (!ValidateCtrlXml()) throw new Exception("Could not validate XML for controls."); // might set the form dirty if it has to make changes
        if (!SetFormTitle()) throw new Exception("Could not set title.");

        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "Ctrl"));
        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "Event"));
        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "FrmMode"));
        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "FrmParam"));
        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "FrmQuery"));
        _g.DatProp.AddSubscriber(new DatPropSubscription(this, _server.ConnectionName, "MacroStep"));

        this.MdiParent = oMDIParent;
        this.WindowState = FormWindowState.Maximized;
        this.Show();

        txtName.Focus();
        txtName.SelectionStart = 0;
        txtName.SelectionLength = txtName.Text.Length;

        tcForm.SelectedTab = tcForm.TabPages["tabDesign"];

        return true;
    }
    catch (Exception ex)
    {
        _g.Errs.Raise(ex);
        return false;
    }
}

Even with the code forcing the form to be WindowState.Normal, it still opens maximized. What am I missing?

Edit: I've made the changes suggested in the "how to avoid screen bouncing" link below. I've now got a different, strange symptom and have created another video of it here: https://youtu.be/MQENr4pNonA

In short, the MDI child appears to open sort-of maximized, but not taking up the whole available area in the MDI parent.

Edit 2: Added the calling code above.

Mark Roworth
  • 409
  • 2
  • 15
  • See here: [Can I show the ToolStrip of a child Form in the MDIParent Form?](https://stackoverflow.com/a/61563839/7444103) and here: [How to avoid screen bouncing when adding a new MDI Child Window](https://stackoverflow.com/a/59674758/7444103). What the Sidebar is and how it's handled is not clear. You can use a docked Panel. – Jimi Feb 16 '21 at 18:18
  • @Jimi - the sidebar is a Panel with Dock=Left with a UserControl in it. I'll read through the two articles above. Thanks. – Mark Roworth Feb 16 '21 at 18:27
  • Well, since the Sidebar is a docked Panel, it should work correctly after the fix. Unless that `SnapToGrid` is doing something weird. Btw, `fd` is undeclared here. – Jimi Feb 16 '21 at 18:28
  • Hi @Jimi - fd is a control on the form as are toolbox, svEvents and svSteps so it shouldn't cause an issue. I've made the modifications as per the fix and am now getting another even stranger symptom. I'll make edits above to explain. – Mark Roworth Feb 16 '21 at 21:32
  • There is something weird there, it's something you don't show, related to your Form. You're also showing this: `this.MdiParent = oMDIParent; this.MainMenuStrip = ((mdi)oMDiParent.MainMenuStrip);`: what? BTW, it's backwards, you need to assign: `[MDIParent].MainMenuStrip = [MDiParent].[TheToolStripObjectInstance];` in the MDIParent's Constructor. Then, when you open a MDIChild Form: `ToolStripManager.Merge([MDIChild].[ToolStripInstance], [MDIParent].[TheToolStripObjectInstance];`. Make a new, clean Project, just do what's shown in those two links. You'll find out what you're doing wrong. – Jimi Feb 17 '21 at 03:49
  • Also, you should explain why you have something like this: `this.MdiParent = oMDIParent; this.MainMenuStrip = ((mdi)oMDiParent.MainMenuStrip);`. What on earth is that cast, `(mdi)`, using a Type (?) / Interface (?) that appears from nowhere, used for nobody knows what when, a command before, you didn't need a cast? -- You have more than one thing there that messes up the standard Form / MDIForm behavior. You're the only one who knows what that is. – Jimi Feb 17 '21 at 04:02
  • Hi @Jimi - ok, so where to start. The code shown above is in the MDI child form. I've also added the calling code. The cast ```(mdi)``` is because the MDI parent is named "mdi". The statement: ```this.MdiParent = oMDIParent;``` is making the child the child of the MDI parent. Note that the child form has no menu or tool strip. There are no toolstrips at all in either the parent or child. The statement ```this.MainMenuStrip = ((mdi)oMDiParent.MainMenuStrip);``` is simply setting the MainMenuStrip of the child to be that of the parent. – Mark Roworth Feb 17 '21 at 10:00
  • I could start to build up from scratch and see where it starts to exhibit the behaviour. The point of posting here really was to hope that someone has seen this before and avoid having to do that; it's a large project. – Mark Roworth Feb 17 '21 at 10:02
  • Ok, so I've found the issue, which I don't understand, but it relates to the fact that on the child MDI form I had ```AutoSizeMode=GrowAndShrink```, rather than ```AutoSizeMode=GrowOnly``` (which is the default). With the latter setting the behaviour appears to be normal. – Mark Roworth Feb 17 '21 at 10:18

0 Answers0