[Webinar] Streamline your web hosting managementRegister Today


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

Posted on 2006-04-04
Medium Priority
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

Accepted Solution

e1v earned 2000 total points
ID: 16380490
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);

Author Comment

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


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

590 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