Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using treeview as menu. the treeview is on the mdiparent. when mdi child is open it goes behid the treeview. How the childform will appear at the top of treeview?
Posted

I'm afraid the fix for this is kind of "ugly," but MDI architecture is kind of "old and in the way now." I'd encourage you, unless you absolutely have to use it, to think of another architecture than MDI.

Perhaps you might consider the ability to set one Form as the 'owner' of other Forms: this will guarantee that these 'owned' Forms will appear at the same level in the z-order when displayed; so they will not be covered over by the 'owner' form. Yep, you give up that custom menu you get with MDI if you go this way, but it's not hard to create on your own.

A control (like your TreeView functioning as menu) on the MDIParent will always appear above any MDIChild Form shown. And, such a control on the MDIParent will also affect the placement of MDI Child Forms:

In your case setting the Left property of any MDIChild Form to #0 will effectively set its left edge to the right border of the TreeView (assuming, as shown here, the TreeView is docked 'Left on the MDIParent Form). But, that still leaves the 'sticky' case of what happens when you maximize an MDI Child Form (assuming you don't want to hide its 'maximize' button).

Here's an idea for a (yes, "ugly") way to work around the issue that the MDI Child Form can "get behind" a control on the MDIParent surface:

First the simple case of the Form being moved by the end-user:
C#
private Form2 f2;

private void Form1_Load(object sender, EventArgs e)
{
    treeView1.Dock = DockStyle.Left;

    f2 = new Form2();

    f2.Move += new EventHandler(f2_Move);
    f2.SizeChanged += new EventHandler(f2_SizeChanged);

    f2.MdiParent = this;
    f2.Show();
}

private void f2_Move(object sender, EventArgs e)
{
    if (f2.Left < 0) f2.Left = 0;
}
And, then, the case of a 'resize' which could result from the user choosing to 'Maximize' the MDI Child Form, as well as click-and-drag:
C#
private void f2_SizeChanged(object sender, EventArgs e)
{
    if (f2.WindowState == FormWindowState.Maximized)
    {
        // must handle Maximized like this unfortunately
        f2.WindowState = FormWindowState.Normal;
        f2.Bounds = f2.MdiParent.ClientRectangle;
        // ugly magic numbers to compensate for form non-client
        // areas ... right and left border areas may vary with
        // OS and features: for example Win7 with Aero 'on' will
        // will have larger right and left borders ...
        f2.Height -= 6;
        f2.Width -= treeView1.Width + 6;
    }

    if (f2.Left < 0) f2.Left = 0;
}
good luck, Bill
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 28-Aug-11 22:15pm    
Very good solution, my 5.

I added my own solution to support your advise to get rid of MDI; your solution credited. Please see.
--SA
Agree with Bill Woodruff: use whatever it takes to get rid of MDI. If is discouraged even by Microsoft. Stop torturing yourself and scaring off your customers.

Some references in support it:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900