Closing

Forms by default can be closed by pressing the cross (X) in the top right corner.
If you want to prevent (or notify) a user from using the cross you can use the FormClosing event.


FormClosing Event

This event occurs as a form is being closed.

private void frmDialog_FormClosing( 

object sender,

   FormClosingEventArgs e) 
{
   e.Cancel = true;
}

The behaviour of this event is different depending on whether you have a modal or modeless windows form.
You can cancel this event to prevent the form closing by setting the Cancel property to True


this.Dispose(); 
this.Close():


Pressing ESC

It is possible to close a windows form by pressing the ESC key
Select the entire form
Look in the Properties window and find the "CancelButton" property
In the drop-down choose "btnClose".


Messages.Question_FormCancelConfirmation 
Messages.Question_FormSaveChanges

private void Method_FormClose() 
private void Method_FormSave()

private void btnClose_Click( 
   object sender,
   EventArgs e)
{
   this._cancelButtonPressed = true;
   this.Close();
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext