In order to make the event of Activate Windows to run only one time, I write the code below
this.Activated-=new System.EventHandler(this.Form1_Activated);
Is it correct but It certain can work well. but it seems it is not a good code.
using System; namespace WindowsApplication1 public class Form1 : System.Windows.Forms.Form private System.ComponentModel.Container components = null; public Form1() InitializeComponent(); } protected override void Dispose( bool disposing ) #region Windows private void InitializeComponent() this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); }
private void Form1_Activated(object sender, System.EventArgs e) |

How Can I make the event of Activate Windows to run only one time?
jjmcguire
I find it better to override the method that actually raises the event, instead of attaching to it. However this is typically a personal preference.
For example:
public class Form1 : Form
{
...
private bool _FirstActivate = true;
protected override OnActivated(EventArgs e)
{
if (_FirstActivate)
{
_FirstActivate = false;
...
}
base.OnActivated(e);
}
}
Ant999
this.Activated-=new System.EventHandler(this.Form1_Activated);
private
void Form1_Activated(object sender, EventArgs e){
this.Activated -= activEventHandler;}
//these two items is to be put in the designer in whidbey
activEventHandler;private System.EventHandler
this
.Activated += (activEventHandler = new System.EventHandler(this.Form1_Activated));sinankoylu
You don't need to have the same instance of the delegate (or event handler) to unhook a method from an event.
Zigzag
Yes, I have test the code, it work well. but I agree with your point, I don't know why it can work well, so I don't think it's a good code!
"here you are removing a NEW event handler for the method, not the one allready added. " I agree with it. I don't know why it can work, could you tell me
Mike Bravo
There is nothing wrong with it. It's the correct way to unregister an Eventhna in C#. If your application requires that the event not be fired again, then you can unregister it the way you have done it.
Regards,
Vikram