title: Got focus and lost focus both event calling repeatedly when handling get and lost focus event of multiple textboxes it goes into infinite loop
body: <p>Following is my code to handle gotfocus and lostfocus event for all textboxes available in form.</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
      {
  
          foreach (Control c in this.Controls)
          {
              if (c is TextBox)
              {
                  c.GotFocus += new System.EventHandler(this.txtGotFocus);
                  c.LostFocus += new System.EventHandler(this.txtLostfocus);
              }
          }
      }
      private void txtGotFocus(object sender, EventArgs e)
      {
          TextBox tb = (TextBox)sender;
          if (tb != null)
          {
              tb.BackColor = Color.Silver;
              tb.BorderStyle = BorderStyle.FixedSingle;
          }
  
      }
      private void txtLostFocus(object sender, EventArgs e)
      {
  
          TextBox tb = (TextBox)sender;
          if (tb != null)
          {
              tb.BackColor = Color.White;
              tb.BorderStyle = BorderStyle.Fixed3D;
          }
      }
  </code></pre>
  
  <p>It works fine with first textbox but when I go to next textbox by pressing tab key it will repeatedly call both events and textbox behave like blinking. After some time error message display in code like:</p>
  
  <blockquote>
    <p>A callback was made on a garbage collected delegate of type 'System.Windows.Forms!System.Windows.Forms.NativeMethods+WndProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.</p>
  </blockquote>
  
  <p>Whats wrong with code? Is there any solution?</p>