I have an MDI application with tab controls. On some machines I get "Error creating window handle" exception when opening up a new tab. This is only occurs at some machines, mostly slow machines. .NET Framework 4.0 is used.
I have been investigating the issue for the last couple of days now and it has driven me nuts! I found the following on MSDN forums The solution by Hans Passant at this MSDN Forum And according to the answer it has something to do with the active MDI child being in maximized state. The given solution is to set the active child in normal window state before showing the new tab and then restore it afterwards. This solution works but I really dislike the flickering that the workaround causes.
The stack trace is as follows:
Error creating window handle:
at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Form.CreateHandle() at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Form.SetVisibleCore(Boolean value) at System.Windows.Forms.Control.Show() at Client.UI.WinForms.Controls.TabManager.OpenNewTab(BaseTab2 tab) in WinForms\Forms\Tabs\TabManager.cs:line 82 at Client.UI.WinForms.Controls.TabManager.OpenTab(BaseTab2 tab) in WinForms\Forms\Tabs\TabManager.cs:line 183 at Client.UI.WinForms.MainForm.buttonLicenses_Click(Object sender, EventArgs e) in WinForms\Forms\MainForm.cs:line 4372
Code:
private void OpenNewTab(BaseTab2 tab)
{
tab.MdiParent = MainForm.Instance;
tab.WindowState = FormWindowState.Maximized;
tab.Show(); <----- [EXCEPTION THROWN HERE]
if (tab.Path != String.Empty)
{
RecentManager.Add(tab.Path);
RecentManager.SetOpen(tab.Path, true);
}
}
UPDATE: Found this at Microsoft support
This can occur when both the following conditions are true.
- The MDI child form contains a control which parents other controls.
- The parent control on the MDI child form removes a child control from its Controls collection in the event handler for the Layout or Resize events.