title: How to improve windows form tools with reusable classes in C#?
body: <p>I am pretty new in the programming world and cannot understand how to use this code from an answer here: <a href="https://stackoverflow.com/questions/86793/how-to-avoid-thousands-of-needless-listview-selectedindexchanged-events">How to avoid thousands of needless ListView.SelectedIndexChanged events</a> by Robert Jeppesen.</p>
  
  <pre><code>public class DoublebufferedListView : System.Windows.Forms.ListView
  {
   private Timer m_changeDelayTimer = null;
   public DoublebufferedListView()
      : base()
   {
      // Set common properties for our listviews
      if (!SystemInformation.TerminalServerSession)
      {
         DoubleBuffered = true;
         SetStyle(ControlStyles.ResizeRedraw, true);
      }
   }
  
   /// &lt;summary&gt;
   /// Make sure to properly dispose of the timer
   /// &lt;/summary&gt;
   /// &lt;param name="disposing"&gt;&lt;/param&gt;
   protected override void Dispose(bool disposing)
   {
      if (disposing &amp;&amp; m_changeDelayTimer != null)
      {
         m_changeDelayTimer.Tick -= ChangeDelayTimerTick;
         m_changeDelayTimer.Dispose();
      }
      base.Dispose(disposing);
   }
  
   /// &lt;summary&gt;
   /// Hack to avoid lots of unnecessary change events by marshaling with a timer:
   /// https://stackoverflow.com/questions/86793/how-to-avoid-thousands-of-needless-listview-selectedindexchanged-events
   /// &lt;/summary&gt;
   /// &lt;param name="e"&gt;&lt;/param&gt;
   protected override void OnSelectedIndexChanged(EventArgs e)
   {
      if (m_changeDelayTimer == null)
      {
         m_changeDelayTimer = new Timer();
         m_changeDelayTimer.Tick += ChangeDelayTimerTick;
         m_changeDelayTimer.Interval = 40;
      }
      // When a new SelectedIndexChanged event arrives, disable, then enable the
      // timer, effectively resetting it, so that after the last one in a batch
      // arrives, there is at least 40 ms before we react, plenty of time 
      // to wait any other selection events in the same batch.
      m_changeDelayTimer.Enabled = false;
      m_changeDelayTimer.Enabled = true;
   }
  
   private void ChangeDelayTimerTick(object sender, EventArgs e)
   {
      m_changeDelayTimer.Enabled = false;
      base.OnSelectedIndexChanged(new EventArgs());
   }
  }
  </code></pre>
  
  <p>So it is a reusable class whose behaviour I would like to add to my already existing ListViews. I am currently managing with the code provided by Ian Boyd on the same url, adding this code to all my ListViews. The problem is that there are so many ListViews already in the ERP program I am currently working on and new ones are coming all the time. Seems like a waste to rewrite the same code all the time.</p>
  
  <pre><code>Timer changeDelayTimer = null;
  
  private void lvResults_SelectedIndexChanged(object sender, EventArgs e)
  {
      if (this.changeDelayTimer == null)
      {
          this.changeDelayTimer = new Timer();
          this.changeDelayTimer.Tick += ChangeDelayTimerTick;
          this.changeDelayTimer.Interval = 200; //200ms is what Explorer uses
      }
      this.changeDelayTimer.Enabled = false;
      this.changeDelayTimer.Enabled = true;
  }
  
  private void ChangeDelayTimerTick(object sender, EventArgs e)
  {
      this.changeDelayTimer.Enabled = false;
      this.changeDelayTimer.Dispose();
      this.changeDelayTimer = null;
  
      //Add original SelectedIndexChanged event handler code here
      //todo
  }
  </code></pre>
  
  <p>I tried to search an answer for this topic from google, but I could not find the right articles by the keywords I was using. Hope someone here can guide me to the right direction on how these 'classes' that are considered as 'components' are to be used with windows form tools.</p>