Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
in this code "e.Cancel" how causes that event canceled.what happen in the background?

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;    //how do this work??

        }
     
    }
Posted

First of all, MSDN states that OnClosing is obsolote and you should use OnFormClosing instead.


Second, when you decide to override a method, don't forget to call the base member (otherwise the form will note fire the event):
protected override void OnClosing(CancelEventArgs e)
{
     //do whatever you want
     ...

     //don't forget to call the base class
     base.OnClosing(e)
}


After you set e.Cancel to true, the event caller (the form) will check that value and decide to process the event if it is true (or not to process it if it false). In your case, if you set the value to false, then the base method OnClosing will not close the form.
 
Share this answer
 
v2
Comments
[no name] 28-Feb-11 8:46am    
namely place that event raised??
Olivier Levrey 28-Feb-11 9:20am    
Sorry I don't understand your question :(
Alan N 28-Feb-11 10:06am    
The original question is tagged as C#1.0 which doesn't have the FormClosing event.
Olivier Levrey 28-Feb-11 10:15am    
You are right. I hadn't noticed that.
Espen Harlinn 28-Feb-11 11:52am    
Anyway, it's still the correct answer - my 5
As Olivier already stated for you, "First of all, MSDN states that OnClosing is obsolote and you should use OnFormClosing instead."

On top of that, the OnFormClosing is the event handler. Setting e.Cancel does not fire a new event, but rather sets a boolean property in the event arguments. When the event gets passed on to the base class using "base.OnClosing(e)", somewhere in the inheritance structure there would be code to read that property and then cancel the event processes if the value is true.
 
Share this answer
 
Comments
Espen Harlinn 28-Feb-11 11:52am    
Nice addition, my 5
Sergey Alexandrovich Kryukov 28-Feb-11 12:50pm    
Good, my 5. It's important to understand that no event is really "canceled", but operation is. It needs some more detail, please see my Answer.
--SA
Sergey Alexandrovich Kryukov 18-May-11 13:38pm    
Not just this.
Also, it should use OnFormClosing and not event (event needs downcast from object sender while the Form is already inherited) + CloseReason should be used).
Please see my updated answer.
--SA
---deleted-- and recommend SAKryukov's solution.

And my updated answer here is an example how the e.cancel works with a custom example.

Here is a class with a custom event which has the CancelEventArgs type of argument.

When this event fires you can set its cancel property which signals the object about further action.

C#
public class PersonWithEvent
{
    public event CancelEventHandler NameAcceptanceEvent;
    private string _name;
    public string ErrMessage{get;set;}
    public string Name
    {
        get { return _name; }
        set
        {
                CancelEventArgs cancelParams = new CancelEventArgs();
                NameAcceptanceEvent.DynamicInvoke(new object[] { this, cancelParams });
                if (cancelParams.Cancel != true)
                {
                    _name = value;
                    ErrMessage = "";
                }
                else
                {
                    ErrMessage = "This name not allowed";
                }
        }
    }

}


Now the acceptance of a name is not encapsulated in this object, but delegated to the user who can implement his own logic for that.

C#
private string personName;
 private void button1_Click(object sender, EventArgs e)
{
    PersonWithEvent person = new PersonWithEvent();
    person.NameAcceptanceEvent+=new CancelEventHandler(person_NameAcceptanceEvent);
    personName = "Monster";
    person.Name = personName;
    MessageBox.Show(person.Name);
    MessageBox.Show(person.ErrMessage);
    personName = "Angel";
    person.Name = personName;
    MessageBox.Show(person.Name);
    MessageBox.Show(person.ErrMessage);
}
private void person_NameAcceptanceEvent(object sender, CancelEventArgs e)
{
    if (personName == "Monster")
    {
        e.Cancel = true;
    }
}




Good luck
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 28-Feb-11 12:52pm    
This is not quite accurate, please see my Answer. Calling Dispose it pointless because later on you won't be able to use disposed form again. I think, this is your mistake.
I did not vote, would suggest you fix your answer.
Also, please see mine.
--SA
Sergey Alexandrovich Kryukov 18-May-11 13:37pm    
Also, it should use OnFormClosing and not event (event needs downcast from object sender while the Form is already inherited) + CloseReason should be used).
Please see my updated answer.
--SA
Albin Abel 19-May-11 4:55am    
Right. I recommend your solution
Sure, OnFormClosing should be used instead.

The operation is pretty simple. No event is really "canceled", what canceled is the operation of closing form. Is the event argument's Cancel == true, form is not closed, so the event FormClosed in never fired. Most typical action used in the event handler is Form.Hide(). It is also good to check up event argument's CloseReason: normally, closing of the form should be prevented (again, replaced with Form.Hide) only for ClosingReason.UserClosing.

[EDIT]

Here is the code sample:
C#
using System.Windows.Forms;

//...

protected override void OnClosing(FormClosingEventArgs e) {
   if (e.CloseReason == CloseReason.UserClosing) {
      this.Hide();
      e.Cancel = true;
   }
}


—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900