MDI child window detaching/reattaching (broken Client Area selection)

I have come across a rather obscure set of issues with Windows Forms in C#, which I believe are related (perhaps the same issue).  It occurs when I have a main form and a set of MDI child forms that it manages.  

The problem: After some time, when clicking on the Client Area (not the title bar portion, but the area where the form's controls are located) of one MDI child form when another is currently selected, it no longer correctly activates and selects the clicked MDI child window.  Only clicking on the MDI child form *title bar* will select that child form.

The most frequent case can occur every time I have an MDI child form.  If I have functionality to detach the form (Listing A) so that it is SDI rather than MDI, it detaches correctly and is viewable in the taskbar.  However, when I reattach that form (Listing B), this problem happens.  Note that in my case, I have a button that has the functionality so that it Attaches/Detaches the MDI form it is inside.

By the way, one other thing that causes this is the AxInterop.SHDocVw Internet Explorer COM dll running inside an MDI Child form in C#.  Once I load that as an MDI child of my main application and click on the browser component (including the ActiveX Security dialog that comes up right away), the problem happens yet again.  Could it be related?

Note that in both (seemingly unrelated) cases, the MDI children no longer are selectable by clicking on the client area of the child form, and this functionality is broken until the application is restarted.  I have some screenshots that I took when I ran Spy++ on the application in the hopes of finding the WM_* or other Windows messaging commands that are called if anyone is interested.  I'd be happy to post them online if anyone would find them to be helpful (although it is pretty easy for any of you to reproduce).  

It seems to me right now that somehow the parent's MDIChildren Forms[] array is becoming corrupted upon detachment or the reattaching is not being done correctly, but I'm not sure.  There seems to be little information written on the subject of SDI <=> MDI form conversion (attaching/detaching) and/or the IE browser control within Windows Forms.

Listing A [Detachment code]:

            public void  DetachMDIChildWindow(Form form_)
            {
                  //I originally did not have this for loop in the code
                  //Not sure if this piece is needed...or what should replace it
                  Form parent = form_.MdiParent;
                  for(int i=0; i< parent.MdiChildren.Length; i++)
                        if(parent.MdiChildren[i] == form_)
                              parent.MdiChildren[i] = null;
                  
                  //reset MDI parent of my form
                  form_.MdiParent = null;
            }

Listing B [ReAttachment code]:
            public void AttachMDIChildWindow(Form form_)
            {
                  form_.MdiParent = FormMain.Instance;
                  form_.StartPosition = FormStartPosition.CenterParent;
                  form_.Activate();
             }

Any help or feedback would be greatly appreciated!  Please note that my main quesion is with respect to the detaching/reattaching of a client MDI form, and I just mentioned the web browser issue since it may be related.

Thanks much,
Dan
LVL 3
Dan_LipsyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

e1vCommented:
Hello, I found a solution to your mdichild activate problem:

http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/tree/browse_frm/thread/e04345db8a2e4b1a/3ce912b01af35254?rnum=1&hl=no&q=Ying-Shen+Yu+mdi&_done=%2Fgroup%2Fmicrosoft.public.dotnet.framework.windowsforms%2Fbrowse_frm%2Fthread%2Fe04345db8a2e4b1a%2F3cebfd2ec25e2a6f%3Flnk%3Dst%26q%3DYing-Shen+Yu+mdi%26rnum%3D4%26hl%3Dno%26#doc_c1c1c1b2054548d3

The post is referring to net 1.1 but I tried it with 2.0 and it worked. I recreated your problem easy without using browser component.
And you do not need the for-loop in DetachMDIChildWindow

Add this to your mdi child form:

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x21:  // WM_MOUSEACTIVATE
                    this.Activate();
                    base.WndProc(ref m);
                    break;


                default:
                    base.WndProc(ref m);
                    break;
            }
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dan_LipsyAuthor Commented:
Thank you so much!  It works great in both cases...

Dan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.