Personally from what you say I would hide all forms until they are
selected -then it doesnt really matter about state, although you can reset
each child forms windowstate when it is reselected from the menu.
I am presuming you are selecting the childforms through a menu using a
MdiList. If this is so then the following wont work because the hidden forms
will not be displayed in the list so you would need to use another mechanism
to select the forms (eg. manually add them and remove them to menu).
--
Another way to "enforce the state" is to use the resize event of the mdi
child form, but the biggest problem with doing this is that I believe
Winforms needs to be able to change the state of the current form in order
to select and display another one.
I agree with Nicholas that maybe what you are doing is not ideally suited to
Mdi forms (especially since I've spent a while playing with your problem
without being entirely satisfied.
I believe the answer to this problem (if you insist on Mdi forms) is to use
a combination of the 2 above methods....
1. implement your own dynamic mechanism to select the forms, and for each
form selection implement an event handler that removes the lock on the
resizing of the form And then activates your selected form.
2. Whenever you add or select a form, change the WindowState to maximized
and add an event handler to lock the resizing of the form.
Please see following code which doesnt do this last bit but gives you a very
good idea about the first two methods. See my custom comments in the TODO
list to go to the pieces of code you will need to uncomment/ comment to try
both methods.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Forms = System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItemAddchild;
private System.Windows.Forms.MenuItem menuItemShowall;
private System.Windows.Forms.MenuItem menuItemWindow;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItemAddchild = new System.Windows.Forms.MenuItem();
this.menuItemShowall = new System.Windows.Forms.MenuItem();
this.menuItemWindow = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemAddchild,
this.menuItemShowall,
this.menuItemWindow});
//
// menuItemAddchild
//
this.menuItemAddchild.Index = 0;
this.menuItemAddchild.Text = "Add Child";
this.menuItemAddchild.Click += new
System.EventHandler(this.menuItemAddchild_Click);
//
// menuItemShowall
//
this.menuItemShowall.Index = 1;
this.menuItemShowall.Text = "Show All";
this.menuItemShowall.Click += new
System.EventHandler(this.menuItemShowall_Click);
//
// menuItemWindow
//
this.menuItemWindow.Index = 2;
this.menuItemWindow.MdiList = true;
this.menuItemWindow.Text = "&Window";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(728, 577);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Mdi Demo";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.MdiChildActivate += new
System.EventHandler(this.Form1_MdiChildActivate);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
foreach (Forms.Form f in this.MdiChildren)
{
f.Dispose();
}
this.Dispose();
}
private void childForm_Resize(object sender, EventArgs e)
{
((Forms.Form) sender).WindowState = Forms.FormWindowState.Maximized;
}
private void Form1_MdiChildActivate(object sender, System.EventArgs e)
{
}
private void menuItemAddchild_Click(object sender, System.EventArgs e)
{
//TODO: comment this to stop hiding mechanism -mechanism unfortunately
prevents the hidden forms appearing in the window list, therefore custom
list will need to be maintained
foreach (Forms.Form f in this.MdiChildren)
{
f.Hide();
}
int formnum = this.MdiChildren.Length; //create new title for form
Forms.Form childForm = new Form();
childForm.Text = formnum.ToString();
childForm.WindowState = Forms.FormWindowState.Maximized;
//TODO: comment this to stop anti-resizing mechanism -mechanism
unfortunately prevents selecting another form from window list because of
prevention of resized, therefore resize event handler will need to be
dynamically added and removed on selection of new form
//childForm.Resize += new EventHandler(childForm_Resize);
childForm.MdiParent = this;
childForm.Show();
}
private void menuItemShowall_Click(object sender, System.EventArgs e)
{
foreach (Forms.Form f in this.MdiChildren)
{
f.Show();
}
}
}
}