title: How can i gain focus again on the textbox without an exception for my following code?
body: <p>I have a form, and i performed validations on my textboxes as specified below..For the below code, when i press the 'clear' button, the textboxes get emptied, and then, When i am trying to focus on any textbox( i.e, i am trying to click on any textbox ) to enter some new text, then an InvalidCastException occurs.. why so ??? </p>
  
  <pre><code>namespace ex_validation
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
                  txtuserid.Validating += new CancelEventHandler(Dovalidation);
                  txtpassword.Validating += new CancelEventHandler(Dovalidation);
                  txtage.Validating += new CancelEventHandler(Dovalidation);
                  btnnextform.Validating += new CancelEventHandler(Dovalidation);
                  btnclear.Validating += new CancelEventHandler(Dovalidation);
              }
              public void Dovalidation(object sender, CancelEventArgs e)
              {
                  TextBox t = (TextBox)sender;// " EXCEPTION OCCURS AT THIS LINE "
                  if (t.Text=="")
                  {
                      t.BackColor = System.Drawing.Color.Yellow;// sets the backcolor of the textbox if found empty
                      e.Cancel = true;// cancel all other events unless user enters something in that relevant textbox
                  }
                  else
                      t.BackColor = System.Drawing.Color.White;
              }
  
              private void txtage_KeyPress(object sender, KeyPressEventArgs e)
              {
                  if (!char.IsDigit(e.KeyChar) &amp;&amp; !char.IsControl(e.KeyChar))
                  {
                      e.Handled = true;
                  }
              }
  
              private void txtage_Leave(object sender, EventArgs e)
              {
                  if (txtage.Text == "")
                      MessageBox.Show("Age field cannot be left blank");
                  else
                  {
                      int x = System.Convert.ToInt16(txtage.Text);
                      if (x &lt; 1 || x &gt; 100)
                      {
                          MessageBox.Show("Age cannot be above 100 OR below 1", "Prompt Box", MessageBoxButtons.OK, MessageBoxIcon.Information);
                          txtage.Clear();
                          txtage.Focus();
                      }
                  }
              }
  
              private void btnnextform_Click(object sender, EventArgs e)
              {
                  Form2 f = new Form2();
                  f.Show();
              }
  
              private void btnclear_Click(object sender, EventArgs e)
              {
                  txtuserid.Text = (string)"";
                  txtpassword.Text = (string)"";
                  txtage.Text = (string)"";
              }
          }
      }
  </code></pre>