avoid closing Windows form

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

How can I stop a form fro closing when the user tries to close it?
For example, in my code:

private void frm_audit_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (_BisSavingData)
{
// Code that will override the Closing method. I don't want the form
to close
}
else
{
// Continue closing the form
}
}
 
I overrode WndProc:

protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int) m.WParam ==
SC_CLOSE)
{
if (_BisSaving)
{
MessageBox.Show("Save in progress. Cannot close!", "ZIPMaster",
MessageBoxButtons.OK, MessageBoxIcon.Hand);
return;
}
}
base.WndProc(ref m);
}
 
VMI said:
I overrode WndProc:

You do not have to do that. Just put something like
form to close

e.Cancel = true;
return;

into your "Form closing" eventhandler.
 
Back
Top