Sunday, March 25, 2012

How to: Close Windows form while doing CreateHandle()

The error Value Close() cannot be called while doing CreateHandle() usually happens when we try to close the form in the constructor or Load event.
For example,

the following code gives the error:

private void frmCustomer_Load(object sender, EventArgs e)
{
if (!Valid())
this.Close;
}

The Solution:

private void frmCustomer_Load(object sender, EventArgs e)
{
if (!Valid())
this.BeginInvoke(new MethodInvoker(Close));

1 comment: