Win32Exception when creating a form that’s minmized and is not shown in the taskbar (Can I be more obscure?)

The Charlotte, NC Microsoft site was closed on both Thursday and Friday due to overwhelming amounts snowfall (12.6 inches were recorded at the CLT airport, 3rd highest recorded snowfall in a 24 hour period for CLT).   I built a 6ft tall snowman with the expert help of Angela, it was a blast!   On my days off I got to play around with some code and came across a problem that someone else out there may one day see:


If you want a repro, create a new winforms application.  Set the WindowState property of the form to Minimized and wire up Resize Event.  In the Resize event handler add the following code (C#):


                  if (this.WindowState == FormWindowState.Minimized)

                        this.ShowInTaskbar = false;


The code above basically removes the form entry from the taskbar when the form reaches its minimized state. 


Once you run the code you’ll see that when you start the application you’ll get the following error:


An unhandled exception of type ‘System.ComponentModel.Win32Exception’ occurred in


Additional information:  Error creating window handle


The faulting callstack:


Void System.Windows.Forms.NativeWindow.CreateHandle(Class System.Windows.Forms.CreateParams)

Void System.Windows.Forms.Control.CreateHandle()

Void System.Windows.Forms.Form.CreateHandle()


Void System.Windows.Forms.Control.SetVisibleCore(Boolean)

Void System.Windows.Forms.Form.SetVisibleCore(Boolean)

Void System.Windows.Forms.Control.set_Visible(Boolean)

Void ystem.Windows.Forms.Application/ThreadContext.RunMessageLoopInner(I4,Class System.Windows.Forms.ApplicationContext)

Void System.Windows.Forms.Application/ThreadContext.RunMessageLoop(I4,Class System.Windows.Forms.ApplicationContext)

Void System.Windows.Forms.Application.Run(Class System.Windows.Forms.Form)

Void WindowsApplication2.Form1.Main()



What I believe to be happening is that since the form does not have handle (ShowInTaskbar = false AND WindowState = Minimized) we throw an exception when trying to create a native window handle.  Of course this exception's not handled.   This problem will not reproduce if the form’s WindowState is set to something else or the ShowInTaskbar property is set to true at the time the forms created (i.e. take out the code within the resize event).

Comments (0)

Skip to main content