Wednesday, 08 September 2010

This was a tricky bug I ran into recently.  First a bit of back history.  I’m working on a largish win-forms MDI application.  Early on in the development I received the  exception “Error creating window handle”  when trying to show the MDI child.  I’m using a 3rd party tab control and I originally thought it was a bug in there.  I found a work-around that if I switched back to the first tab (tab index 0) before showing the new child, the error seemed to go away.  For many months, this worked fine.  Recently I went through a bit of refactoring of the application and suddenly this bug reappeared despite my hack.   Well to make  a long story short some more research ensued and I found the fix (hopefully the true fix now)…actually it is a bit of workaround as well, but we’ll just have to live with it for now.  It appears that .NET MDI applications hates a maximized child window.  To bring another tab to the front, it has to restore the active child window from the maximized state, which triggers the forms layout method to re-layout the form.  This causes a series of events (not interested in digging too deep) that basically seems to create your form twice causing a NullReference exception at some point.  More details here

 

Some code that might help me in the future:

 

    public void DisplayChild(Form child) {
      bool max = false;
      if (this.ActiveMdiChild != null && this.ActiveMdiChild.WindowState == FormWindowState.Maximized) {
        this.ActiveMdiChild.WindowState = FormWindowState.Normal;
        max = true;
      }
      child.MdiParent = this;
      child.Show();
      if (max) child.WindowState = FormWindowState.Maximized;
    }

Wednesday, 08 September 2010 12:48:57 (Eastern Daylight Time, UTC-04:00) | Comments [1] | Code#
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: