8
votes

I am creating a C# WinForms MDI application. I have a main form which contains 4 other forms inside it. I want to be able to move the child forms outside of the parent form (their FormBorderStyle value is set to sizeable toolbar so that separate windows don't appear on the taskbar for each child window). I am able to accomplish this by using the following code for the main form:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Prototype
    {
       public partial class MdiParent : Form
       {
           private FastEyeControl m_ControlForm;
           private FastEyeLogWindow m_LogForm;
           private FastEyeConfiguration m_ConfigurationForm;
           private ROCOrderWindow m_OrderLogForm;

           public MdiParent()
           {
               InitializeComponent();

               m_ControlForm       = new FastEyeControl();
               m_LogForm           = new FastEyeLogWindow();
               m_ConfigurationForm = new FastEyeConfiguration();
               m_OrderLogForm      = new ROCOrderWindow();
           }

           private void MdiParent_Load(object sender, EventArgs e)
           {
               m_ControlForm.Show(this);
               m_LogForm.Show(this);
               m_ConfigurationForm.Show(this);
               m_OrderLogForm.Show(this);
           }
        }
    }

However, when I minimize the parent form, all child forms get minimized as well (as expected). Is there anyway to prevent any child forms that are outside of the parent window to be minimized when the parent window is minimized? Basically, I want the user to be able to resize and move the individual child forms outside of the parent form if desired (like undocking a toolbar in Visual Studio.NET and placing it in another monitor somewhere). Thanks for your help!

3
Great question. I don't have the answer but I'll look into it. That is definitely an interesting scenario.IAmTimCorey
Did you try removing "this" from the Show() call? (Showing the form without a parent.)Josh M.
Yes, but then what happens is that the child forms are placed behind the parent window on startup. I want the child forms to always be in front of the parent form.Andrew
Are you sure you are working with MDI parent/children? As far as I know, you need to set the MdiParent property for each child in order to get the true MDI feature working. As far as I see, you are only setting the Owner of the child form.Adi
Yes, but if I use MdiParent then the child forms cannot move outside of the parent form.Andrew

3 Answers

1
votes

I think you will have to move away from owned/parented/mdi windows, and will instead have to make them all unowned top-level windows, in "parallel" to each other. And then write your own logic for docking one window within another.

1
votes

You haven't actually created an MDI application, you don't set the child forms' MdiParent property. Should be clearly visible, you can move the child form outside of the bounds of the main form.

You made them owned windows by using the Show(owner) overload. Which means that they will be always on top of the main window. And get minimized too when you minimize the main window.

Simply call the Show() method instead (no argument).

1
votes

OK I got it to work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Prototype
{
    public partial class MdiParent : Form
    {
        private FastEyeControl m_ControlForm;
        private FastEyeLogWindow m_LogForm;
        private FastEyeConfiguration m_ConfigurationForm;
        private ROCOrderWindow m_OrderLogForm;

        private Point m_ControlFormLocation;
        private Point m_LogFormLocation;
        private Point m_ConfigurationFormLocation;
        private Point m_OrderLogFormLocation;

        public MdiParent()
        {
            InitializeComponent();

            m_ControlForm       = new FastEyeControl();
            m_LogForm           = new FastEyeLogWindow();
            m_ConfigurationForm = new FastEyeConfiguration();
            m_OrderLogForm      = new ROCOrderWindow();

            m_ControlFormLocation = new Point(0, 25);
            m_LogFormLocation = new Point(0, 405);
            m_ConfigurationFormLocation = new Point(550, 25);
            m_OrderLogFormLocation = new Point(0, 630);
        }

        private void MdiParent_Load(object sender, EventArgs e)
        {
            DockForm(m_ControlForm, m_ControlFormLocation);
            DockForm(m_LogForm, m_LogFormLocation);
            DockForm(m_ConfigurationForm, m_ConfigurationFormLocation);
            DockForm(m_OrderLogForm, m_OrderLogFormLocation);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DockForm(Form form, Point location)
        {
            form.TopLevel = false;
            form.Location = location;

            if (! this.Controls.Contains(form))
            {
                this.Controls.Add(form);
            }

            form.Visible = true;
        }

        private void UndockForm(Form form)
        {
            if (this.Controls.Contains(form))
            {
                this.Controls.Remove(form);
            }

            form.TopLevel = true;
            form.Visible = true;
        }

        private void DockOrUndockForm(Form form, Point location)
        {
            if (this.Controls.Contains(form))
            {
                UndockForm(form);
            }
            else
            {
                DockForm(form, location);
            }
        }

        private void ToggleDockingOrDockForm(Form form, Point location)
        {
            if (form.Visible)
            {
                DockOrUndockForm(form, location);
            }
            else
            {
                DockForm(form, location);
            }
        }

        private void fastEyeControlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleDockingOrDockForm(m_ControlForm, m_ControlFormLocation);
        }

        private void fastEyeLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleDockingOrDockForm(m_LogForm, m_LogFormLocation);
        }

        private void fastEyeConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleDockingOrDockForm(m_ConfigurationForm, m_ConfigurationFormLocation);
        }

        private void rOCOrderLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleDockingOrDockForm(m_OrderLogForm, m_OrderLogFormLocation);
        }
    }
}

Is this code safe?