C # WinForms: how do you prevent minimizing a child form while minimizing the parent form?

I am creating a C # WinForms MDI application. I have a basic form that contains 4 more forms. I want to be able to move child forms outside the parent form (their FormBorderStyle value is set on the dimensional toolbar so that individual windows do not appear on the taskbar for each child window). I can accomplish this 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 are also minimized (as expected). Is there a way to prevent child forms outside the parent window from minimizing the parent window? Basically, I want the user to be able to resize and move individual child forms outside the parent form (for example, undock the toolbar in Visual Studio.NET and place it on another monitor somewhere). Thanks for your help!

+6
source share
3 answers

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?

+1
source

I think you will have to move away from the owned / parent / mdi windows, but instead have to make them all unoccupied top-level windows that are "parallel" to each other. And then write your own logic to dock one window in another.

+1
source

In fact, you did not create an MDI application, you do not set the MdiParent property of child forms. Should be clearly visible, you can move the child form outside of the main form.

You made them belong to windows using Show (owner) overload. This means that they will always be on top of the main window. And minimize when you minimize the main window.

Just call the Show () method (no argument).

+1
source

Source: https://habr.com/ru/post/887237/


All Articles