MDI child window detaching/reattaching (broken Client Area selection)
Posted on 2006-04-04
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.