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

Posted on 2006-04-04
Last Modified: 2008-01-09
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;

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,
Question by:Dan_Lipsy
    LVL 6

    Accepted Solution

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

    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
                        base.WndProc(ref m);

                        base.WndProc(ref m);
    LVL 3

    Author Comment

    Thank you so much!  It works great in both cases...


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now