title: C# Textbox Keypress Event?
body: <p>I was wondering how can I make the textbox only accepts less than or equal to a number?</p>
  
  <p>I have this keypress event for my textbox</p>
  
  <pre><code>//**This will select and count the number of rows for a certain topic**
  
  OleDbCommand command = new OleDbCommand();
  command.Connection = connection;
  command.CommandText = @"SELECT COUNT(CONTENT) FROM qPIPE WHERE CONTENT = '" + topic + "'";
  OleDbDataAdapter dAdap = new OleDbDataAdapter(command);
  DataTable dTable = new DataTable();
  dAdap.Fill(dTable);
  
  private void txtNo_KeyPress(object sender, KeyPressEventArgs e)
  {
      topic = cmbTopic.Text;
  
      //**This is to get the cell value of the DataTable**
      int total = Int32.Parse(dTable.Rows[0][0].ToString());
  
      //**Int32.Parse(txtNo.Text) &gt;= total, convert the txtNo.Text to integer and compare it to total (total number of rows), ideally**
      if (!char.IsControl(e.KeyChar) &amp;&amp; !char.IsDigit(e.KeyChar) &amp;&amp; Int32.Parse(txtNo.Text) &gt;= total
  
      {
          System.Media.SystemSounds.Beep.Play(); //**Plays a beep sound to alert an error**
          e.Handled = true; //**Prevent the character from being entered**
      }
  }
  </code></pre>
  
  <p>For my IF statement, the user is only allowed to input a numeric/integer and must be less than or equal to a number. And when I run the program, yes it doesn't accept other characters except a number, but I can input greater than the total number.</p>