|
-
October 30th, 2009, 10:07 AM
#1
unhooking event handlers
I need to manually unhook the event in my code. I have searched my code and found many references to:
this.Loaded += method
What I'm not 100% clear on is where in my code should I put the
this.loaded -= method
to ensure that I have unhooked the event handler. This is most often occuring when opening an window or a button is being created.
Does this go in my code when I close my window or at the end of the method call, a little guidence would be most appreciated.
Thanks
CHS
-
October 30th, 2009, 10:38 AM
#2
Re: unhooking event handlers
If the event you are referring to is form.Load then it will only be called once.
Its not quite clear what you are asking or that you dont have a clear enough idea as to what events are however I will try to explain something else which will hopefully cover your question.
Take an example that you have created an application that will do nothing more than display a CheckBox to the user, when the user changes the check state of the checkbox a MessageBox pops up telling the user that the state has been changed.
Code:
private void chkBox_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("The state of the check box has been changed to " + chkBox.Checked ? "Checked" : "Unchecked");
}
Now say that you want the application to remember the check state between different instances.Ie when you close the application it saves the state of the checkbox and when it is starts again it applies the check state that was applied when it closed.Ignoring how to save\load and assume GetSavedCheckState() reads from the file and returns a bool which is whether or not the check box was checked on close .
Code:
private void MainForm_Load(object sender, EventArgs e)
{
this.chkBox.Checked = GetSavedCheckState();
}
This however will now cause the event CheckedChanged to be fired thus displaying the MessageBox to the user.So we want to load the data apply the check but dont popup the message box.
Code:
private void MainForm_Load(object sender, EventArgs e)
{
this.chkBoxReturn.CheckedChanged -= new System.EventHandler(this.chkBox_CheckedChanged); //Remove
this.chkBox.Checked = GetSavedCheckState();
this.chkBoxReturn.CheckedChanged += new System.EventHandler(this.chkBoxReturn_CheckedChanged); //Reapply
}
This example assumes that you applied the event listener at the start eg. through the IDE, if you wish to think about it in a slightly different way, instead of applying the saved data when your form loads apply the check when the user presses a button, so up to the point of pressing the button and in turn loading the previously saved data changing the Checkbox state will fire the event and show the MessageBox but when the button is press it is "silently" changed.
Hopefully this answers your question.
Ger.
-
October 31st, 2009, 09:23 AM
#3
Re: unhooking event handlers
Thank You Ger, this is really helpful. I understand my initial explaintion was not good, next time I will be more clear. But you were able to figure out my need and answer accordingly, I will look to work on this immediately.
Thanks again!
CHS
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|