Unregistered event with - = - Operator

When the window opens, I register a Deleted-event handler on my business object. It is passed to the constructor as business

:

business.Deleted += new EventHandler<EventArgs>(business_Deleted);

      

The user can now click a button to delete it (by deleting the entry, you know). An event handler is registered to capture the deletion by other editor windows and notify the user ("Item deleted in another editor window.").

If the user deletes it in the current window, this message would be silly, so I would like to unregister the event sooner:

Business business = (Business)businessBindingSource.DataSource;
business.Deleted -= new EventHandler<EventArgs>(business_Deleted);

      

My problem is simple: the message is displayed anyway, so unregistering doesn't work. I tried to keep the EventHandler in a separate element. Does not work.

Any help would be great.

Matthias

PS Reading this post , I'm afraid that proper unregistering of the event might make it unregistered for all editor windows. Maybe the next problem .; -)

0


source to share


5 answers


If you really want this behavior (I don't think this is a good pattern, be unimportant) you can get from the EventArgs class and add a property for the author of the deletion. Then you can do:

c.Delete( this ); //this = window
// ...
void business_Deleted(object sender, EventArgs e) {
    bool isDeletedFromMe = false;
    if ( e is DeletedEventArgs ) { isDeletedFromMe = object.ReferenceEquals( this, e.Author ); }
    if ( false == isDeletedFromMe ) {
        MessageBox.Show("Item has been deleted in another editor window.",
            "...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        Close();
    }
}

      



or you can do it like this:

void business_Deleted(object sender, EventArgs e)
{
    if ( false == object.ReferenceEquals( sender, this.currentlyDeletingBusiness ) ) {
        MessageBox.Show("Item has been deleted in another editor window.",
            "...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    Close();
}

Business currentlyDeletingBusiness;
private void deleteButton_Activate(object sender, EventArgs e)
{
    Business c = (Business)businessBindingSource.DataSource;
    try {
        this.currentlyDeletingBusiness = c;
        c.Delete();
    }
    finally {
        this.currentlyDeletingBusiness = null;
    }
}

      

+1


source


I'm not sure why your example code is not working as expected, but you can try adding a private member variable to check if this user is deleting the entry or another user.



private bool otherUser = true;

void business_Deleted(object sender, EventArgs e)
{
    if(otherUser) {
        /* Show message */
    }
}

void deleteButton_Activate(object sender, EventArgs e)
{
    otherUser = false;
    /* Delete record */
}

      

+1


source


Can an event be logged more than once? I would put a breakpoint after

business.Deleted -= new EventHandler(business_Deleted);
      

and checked _invocationCount and _invocationList of business.Deleted (under Base -> Non-Public) to make sure no more events were logged.
+1


source


From a few lines I can see, this might be the problem:

Business business = (Business)businessBindingSource.DataSource;

      

It looks like you are changing the link owned by the business to another object. Perhaps yours was just an example and you use the same object every time.

0


source


Of course, here is the shortest possible full sample:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication
{
    public partial class BusinessEditor : Form
    {
        private EventHandler<EventArgs> businessDeletedHandler;

        public BusinessEditor(Business business)
            : this()
        {
            InitializeComponent();

            businessBindingSource.DataSource = business;

            // Registering
            businessDeletedHandler = new EventHandler<EventArgs>(business_Deleted);
            business.Deleted += businessDeletedHandler;
        }

        void business_Deleted(object sender, EventArgs e)
        {
            MessageBox.Show("Item has been deleted in another editor window.",
                "...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            Close();
        }

        private void deleteButton_Activate(object sender, EventArgs e)
        {
            Business c = (Business)businessBindingSource.DataSource;
            // Unregistering
            c.Deleted -= businessDeletedHandler;
            c.Delete();
            Close();
        }
    }
}

      

I think it must be the same instance, Ed. Am I right with that?

Greetings and thank you!

0


source







All Articles