Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WinForms: How do you prevent child form from being minimized when parent form is minimized?

Tags:

c#

winforms

mdi

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!

like image 313
Andrew Avatar asked May 03 '11 21:05

Andrew


3 Answers

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.

like image 87
Vilx- Avatar answered Nov 15 '22 03:11

Vilx-


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).

like image 26
Hans Passant Avatar answered Nov 15 '22 03:11

Hans Passant


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?

like image 41
Andrew Avatar answered Nov 15 '22 04:11

Andrew