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.
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