How Can I make the event of Activate Windows to run only one time?

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;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{

 public class Form1 : System.Windows.Forms.Form
 {

  private System.ComponentModel.Container components = null;

  public Form1()
  {

   InitializeComponent();

  }

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows

  private void InitializeComponent()
  {

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Activated += new System.EventHandler(this.Form1_Activated);

  }
  #endregion


  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Activated(object sender, System.EventArgs e)
  {
   this.Activated-=new System.EventHandler(this.Form1_Activated);
  }
 }
}


 




Answer this question

How Can I make the event of Activate Windows to run only one time?

  • jjmcguire

    The code is certainly correct.

    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

    Have you tested it My initial reaction was that this can't work.

    this.Activated-=new System.EventHandler(this.Form1_Activated);
     
    here you are removing a NEW event handler for the method, not the one allready added.


    private void Form1_Activated(object sender, EventArgs e)

    {

    this.Activated -= activEventHandler;

    }

    //these two items is to be put in the designer in whidbey
    private
    System.EventHandler activEventHandler;

    this.Activated += (activEventHandler = new System.EventHandler(this.Form1_Activated));



     

  • sinankoylu

    No the code is correct.

    You don't need to have the same instance of the delegate (or event handler) to unhook a method from an event.

  • Zigzag

    RE. here you are removing a NEW event handler for the method, not the one allready added.

    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

    Hi,

    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

  • How Can I make the event of Activate Windows to run only one time?