title: Focus leave text box
body: <p>I have a text box <code>TB</code> and a handler <code>EH</code> for the event Focus / Leave.</p>
  
  <p>Also, I have a button <code>BT</code> to quit the program when clicked, only dispose();
  If I leave <code>TB</code> box without proper data in it, the focus leave event handler is triggered to check the data, it warns me and return focus back to <code>TB</code>.</p>
  
  <p>But if I want to quit the program while <code>TB</code> has the focus and click <code>BT</code>, again the <code>EH</code> is triggered and returns the focus to <code>TB</code> and the program won't quit.</p>
  
  <p>How can I solve this matter?  Here's the code :</p>
  
  <pre><code>public Form1()
  {
      InitializeComponent();
  }
  
  private void EH(object sender, EventArgs e) // event handler EH
  {
      double temp;
      if (TB.Text == "")
      {
          MessageBox.Show("Must enter a valid distance for d1!\r\n" +
              "The valid range is ( 10,32 )",
              "Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
              TB.Focus();
              return;
      }
      else
      try
      {
          temp = Convert.ToDouble(TB.Text);
          if (temp &lt; 10 || temp &gt; 32)
          {
              MessageBox.Show("Invalid distance for d1!\r\n" +
              "The valid range is ( 10,32 )",
              "Again! Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  TB.Text = "";
                  TB.Focus();
                  return;
          }
          minh1 = 1 / 8 * temp; // sets minimum h1
              if (minh1 &lt; 10)
                  minh1 = 10;
      }
      catch (Exception) // can't convert
      {
          MessageBox.Show("Invalid numeric entry!\r\n" +
          "Please enter a valid number!",
          "Hey! Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
           TB.Text = "";
           TB.Focus();
      }
  }
  
  private void TB_TextChanged(object sender, EventArgs e) // change text in TB
  {
      if (TB.Text == "")
          btgo.Enabled = false;
      else
          btgo.Enabled = true;
  }
  private void btgo_Click(object sender, EventArgs e) // Execute!
  {
      say.Text = "Minimum height h1 has been calculated to be " +
      string.Format("{0:#0.00}", minh1) + " Fts";
      BT.Focus();
  }
  private void BT_Click(object sender, EventArgs e) // --- PROGRAM END ---
  {
      Dispose();
  }
  </code></pre>