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#
Tuesday, 13 July 2010

I ran into a situation where I needed to know what control was gaining focus in the Leave() event of another control.  The situation I was working with is as follows;  I have a multi-line text box that when it gains focus I want a listview of potential values to popup below it.  When the textbox loses focus, the listview should be hidden.   The user should be able to click on the listview to select items to input into the textbox.  The problem was when the user clicked on the listview, the textbox lost focus and was hiding the listview. 

 

Enter the ActiveControl property on the form.  This handy property allows me to check, in the Leave() event, what is the new active control with focus.  Thus I can check to see if the textbox lost the focus to the listview control and if so, skip the hiding of it. 

 

Here we have the textbox named txtComplaint, when it gets the focus we make listView visible.

 

private void txtComplaint_Enter(object sender, System.EventArgs e)
{
  listView.Visible = true;
}

 

When the textbox loses focus we see if the ActiveControl is the listView, if not we hide it.

private void txtComplaint_Leave(object sender, System.EventArgs e)
{
  if (ActiveControl.Name != "listView")
    treeComplaint.Visible = false;
}
Tuesday, 13 July 2010 15:56:56 (Eastern Daylight Time, UTC-04:00) | Comments [0] | .NET | Code#
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: