Search code examples
c#visual-studio-2012user-controlsevent-handlingformclosing

Replacing Obsolete Closing Eventargs with new FormClosing args


We are currently in the process of updating our database system and all of the files by building them(32bit-2005 VC#) to 64 bit now. Something I've come across is the leaking of data when an assignment using this.closing += new CancelEventArgs(Event Name); I've found it to be obsolete as fellow colleagues have put it and am looking for a way to switch to using the FormClosing event.

...

Old Code and Event Ex.

this.Closing += new CancelEventHandler(AssignUsers_Closing);


private void AssignUsers_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    try
    {
        "some event"

    }
    catch (Exception ex)
    {
        ShowException se = new ShowException();
        se.ShowDialog(ex);
    }
}

This is where I'd like to convert to the FormClosing Event so that windows can take care of the data leak for me rather than having to add a -= statement for each event like this to the .dispose() function.

Idea/New Code

private void AssignUserForm_FormClosing(object sender, FormClosingEventArgs e)
{
    try
    {
        *code*
        // Use e.cancel to test whether to close the form or not.
    }
    catch (Exception ex)
    {
        ShowException se = new ShowException();
        se.ShowDialog(ex);
    }
}

Would this be a viable alternative. Any suggestions on how to make this change?

EDIT: BETTER OVERVIEW OF MY QUESTION

Changing this:

public AssignUserForm()
        {
            InitializeComponent();
            InitMe();

            try
            {
                Database.ApplyFieldSecurity(this);
            }

            catch { }
        }

        private void InitMe()
        {
            try
            {
                this.Closing += new CancelEventHandler(AssignUsers_Closing);

                // Get the users from the system


               //Binding happens

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Turning it into something where I can use Windows Events to simply create a FormClosing event.


Solution

  • The problems were solved. There was a data leak occurring between our database and when the control that this dialog belonged to were loading. I appreciate all of the help. The database shot over the information before the dialog loaded, and then would send it over again. Only one set of the data was properly disposed of. We fixed the problem. Thank you. –