Windows Develop Bookmark and Share   
 index > Windows Forms General > Arranging MDI Child forms
 

Arranging MDI Child forms

I have an MDI application in which I want the user to drag the child forms to put them in order. I will have to write some mouse handling code for that I know. However when you click on the child forms, before you even capture the mouse , they change their order! I want them to stay still and move only when the code tells them to.
Try it! Just put this code into a default form1 - I'm using C# Express 2008. Then click on a child form and watch it move to the bottom - slippery little devil.

using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int ii = 1; ii < 5; ii++)
{
Form cForm = new Form();
cForm.MdiParent=this;
cForm.Dock = DockStyle.Top;
cForm.Size = new Size(120, 40);
cForm.Text = "Child " + ii.ToString();
cForm.MouseDown += new MouseEventHandler(cForm_MouseDown);
cForm.Show();
}
}

void cForm_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("Mouse Down");
}
}
}

I want the child forms to stay still and the mouse click to simply trigger the Event handler.

Any help greatly appreciated,
Andrew Parsons
aparsons888  Friday, December 21, 2007 5:42 PM

The problem lies in the setting of Dock property to DockStyle.Top. Do not set this property would make the forms stay still when clicking. However, if you stillwant the Dock style, there's a trick to get the same feature, i.e. to set the Anchor property and hard-code the location for the forms, something like this, see the code in bold

Code Block

Form1_Load(object sender, EventArgs e)

{

this.IsMdiContainer = true;

for (int ii = 1; ii < 5; ii++)

{

Form cForm = new Form();

cForm.MdiParent = this;

//cForm.Dock = DockStyle.Top;

cForm.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;

cForm.Size = new Size(this.Width, 40);

cForm.Top = (ii - 1) * cForm.Height;

cForm.Text = "Child " + ii.ToString();

cForm.MouseDown += new MouseEventHandler(cForm_MouseDown);

cForm.Show();

}

}

void cForm_MouseDown(object sender, MouseEventArgs e)

{

MessageBox.Show("Mouse Down");

}

Zhi-Xin Ye  Thursday, December 27, 2007 8:11 AM

The problem lies in the setting of Dock property to DockStyle.Top. Do not set this property would make the forms stay still when clicking. However, if you stillwant the Dock style, there's a trick to get the same feature, i.e. to set the Anchor property and hard-code the location for the forms, something like this, see the code in bold

Code Block

Form1_Load(object sender, EventArgs e)

{

this.IsMdiContainer = true;

for (int ii = 1; ii < 5; ii++)

{

Form cForm = new Form();

cForm.MdiParent = this;

//cForm.Dock = DockStyle.Top;

cForm.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;

cForm.Size = new Size(this.Width, 40);

cForm.Top = (ii - 1) * cForm.Height;

cForm.Text = "Child " + ii.ToString();

cForm.MouseDown += new MouseEventHandler(cForm_MouseDown);

cForm.Show();

}

}

void cForm_MouseDown(object sender, MouseEventArgs e)

{

MessageBox.Show("Mouse Down");

}

Zhi-Xin Ye  Thursday, December 27, 2007 8:11 AM
Many thanks indeed for your kind solution to this. It gives me a very promising direction to pursue and I'm pretty sure I can now get were I want to go.
My aim is for the user to arrange ( and size ) the child forms by clicking and dragging - very much the way it works in Sony Vegas. Any pointers on best practice for achieving this would be handy - I will continue to code it anyway. Thanks again,
Kind Regards,
Andrew Parsons
aparsons888  Monday, December 31, 2007 10:17 AM

You can use google to search for other answers

Custom Search

More Threads

• What are the possible values returned from the RichTextBox.UndoActionName/.RedoActionName propetry?
• Showing multiple window forms on one form?
• MessageBox.Show Question
• view console window in WindowApplication solution?
• How to test if a modeless form already exists?
• Why design-time theme gone?
• Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
• Custom Context Menu Strip
• Error when binding DataGridViewComboBoxColumn to a datasource
• Could not find any resources appropriate for the specified culture or the neutral culture