Mdi Child Forms functionality does NOT work correctly in Windows F



Ok, here is the problem

When two MDI Child forms are created in a MDI Parent you can switch between
the two child forms by clicking anywhere on the child forms (Not just their
The child forms basically change their Z Order within the Mdi Parent and the
input focus shifts. This is all Good

However if the Child Forms' Handle has been created before assigning them as
MDI Child forms then you cannot switch between them by clicking anywhere on
the child forms. What makes this worse is the fact that now when you click
between child forms (Not on their titlebars) the input focus shifts but the
forms do not change their Z order.

The following 5 min example illustrates this problem. I am using C# for my
example. Here are the steps that I took

1) Start a C# Windows Form Application in .NET

2) For the Default Form1 that is created set the "IsMdiContainer" property
to true in the properties window.

3) Add another form "Form2" to the application and add a textBox to it

4) Now add a Button to the Mdi Parent Form (Form1), Change the caption of
the button to "Child" and add the following code in the click event of this
button :

Form2 frm = new Form2();
frm.MdiParent = this;

5) Now Run the application. When the MDI Parent Form Shows up, click the
button on it Twice. Two MDI Child windows will show up in the mdi container.
Switch between the two by clicking in their textboxes (Not the titlebars) and
you will see that the input focus changes. Note that the child form that has
the input focus has an active title bar, i.e the two child forms toggle back
and forth in their Zorder

6) Now stop the application and change the code written in step 4 above as

Form2 frm = new Form2();
IntPtr handle = frm.Handle; // This will force the handle
creation on the form
frm.MdiParent = this;

7) Now run the application again. Once again click the button on the MDI
Parent twice. Two child windows show up. Try switching between them by
clicking in their textboxes. The Input focus moves, but the highlight on the
Mdi child windows does not. In fact if you cascade them you will see that the
forms do NOT change their Z Order. But if you click on their titlebars then
they work fine.

Can someone please explain this?

This is a subtle behavior but it open a can of worms. In the good old days
of VB programming (prior to .NET) you never changed the MDI-ness of a child
form after it has been created for the same kind of reasons.

Now in .NET since the MDIParent property can be changed anytime (the
documentation does not say that change it before creating a handle on the
form), this makes the MDI behavior inconsistent.



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question