472,119 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

MDI Child Window Problem

Hi,

I have an MDI application which has aboout 10 child windows. The way
the app needs to work is that only one window should be visible at a
time and it should be maximized within the parent window. I have set
all my child windows to be WindowState.Maximized but after showing 2
or 3 windows, the windows all drop back to Normal state.

I have tried various things to overcome this inclusing overriding
OnResize etc but none seem to give me the desired effect.

Can anyone suggest how I might achieve this?

Cheers

James
Nov 16 '05 #1
3 5498
Hi James,

Could you show us some sample code? I believe something in your code may trigger a WindowState reset or a Cascade.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
James,

I would not recommend an MDI window for this. Rather, I would have a
tabbed dialog, or something of that nature, where the window changes with
each step. You are trying to make the MDI application do something that it
inherently should not.

In your situation, take the logic and the controls on the child forms
and place them in individual controls, and then show and hide the controls
in one window as needed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James Spibey" <ja**********@gmail.com> wrote in message
news:c2**************************@posting.google.c om...
Hi,

I have an MDI application which has aboout 10 child windows. The way
the app needs to work is that only one window should be visible at a
time and it should be maximized within the parent window. I have set
all my child windows to be WindowState.Maximized but after showing 2
or 3 windows, the windows all drop back to Normal state.

I have tried various things to overcome this inclusing overriding
OnResize etc but none seem to give me the desired effect.

Can anyone suggest how I might achieve this?

Cheers

James

Nov 16 '05 #3
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.Form 1_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();
}
}
}
}


"James Spibey" <ja**********@gmail.com> wrote in message
news:c2**************************@posting.google.c om...
Hi,

I have an MDI application which has aboout 10 child windows. The way
the app needs to work is that only one window should be visible at a
time and it should be maximized within the parent window. I have set
all my child windows to be WindowState.Maximized but after showing 2
or 3 windows, the windows all drop back to Normal state.

I have tried various things to overcome this inclusing overriding
OnResize etc but none seem to give me the desired effect.

Can anyone suggest how I might achieve this?

Cheers

James

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bonj | last post: by
1 post views Thread by Bill Borg | last post: by
4 posts views Thread by Steve Barnett | last post: by
4 posts views Thread by Richard Lewis Haggard | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.