locked
Allow user to only enter numbers in textbox and if character entered then clear the textbox along with a message RRS feed

  • Question

  • Hey guys, How can i allow users to enter only numbers in a textbox? And if they enter a character then i erase that character and give a message?

    I tried this:

    private void txtInventoryManagerCostPrice_KeyPress(object sender, KeyPressEventArgs e)
              {
                  char keypress = e.KeyChar;
                  if (keypress == Convert.ToChar(Keys.D0) || keypress == Convert.ToChar(Keys.D1) || keypress == Convert.ToChar(Keys.D2) || keypress == Convert.ToChar(Keys.D3) || keypress == Convert.ToChar(Keys.D4) || keypress == Convert.ToChar(Keys.D5) || keypress == Convert.ToChar(Keys.D6) || keypress == Convert.ToChar(Keys.D7) || keypress == Convert.ToChar(Keys.D8) || keypress == Convert.ToChar(Keys.D9) || keypress == Convert.ToChar(Keys.NumPad0) || keypress == Convert.ToChar(Keys.NumPad1) || keypress == Convert.ToChar(Keys.NumPad2) || keypress == Convert.ToChar(Keys.NumPad3) || keypress == Convert.ToChar(Keys.NumPad4) || keypress == Convert.ToChar(Keys.NumPad5) || keypress == Convert.ToChar(Keys.NumPad6) || keypress == Convert.ToChar(Keys.NumPad7) || keypress == Convert.ToChar(Keys.NumPad8) || keypress == Convert.ToChar(Keys.NumPad9))
                  {
    
                  }
                  else if (keypress == Convert.ToChar(Keys.Back))
                  {
    
                  }
                  else
                  {
                      MessageBox.Show("You Can Only Enter A Number!");
                      txtInventoryManagerCostPrice.Text = "";
                  }
              }

    But this code allows characters 'a' through 'i'. Which i don't want. Help would be appreciated.

    Regards,

    Rayyan Tahir

    Wednesday, July 4, 2012 12:13 PM

Answers

  • Hi try this code

     char keypress = e.KeyChar;
                if (char.IsDigit(keypress) || e.KeyChar==Convert.ToChar(Keys.Back))
                {
                
    
                }
                else
                {
                    MessageBox.Show("You Can Only Enter A Number!");
                    e.Handled = true;
                }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by san Sanz Wednesday, July 4, 2012 12:34 PM
    • Marked as answer by CoOlDud3 Wednesday, July 4, 2012 2:14 PM
    Wednesday, July 4, 2012 12:34 PM

All replies

  • This is actually a common task - instead of re-writing something like this you will find that people have been nice and already created this control for you.

    Microsoft's .Net 4 Masked TextBox: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx

    //Add to the textbox's KeyPress event
    private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
    e.Handled = true;
    }

    Add your message in the above event.

    Another nice NumericTextBox control from the code project: http://www.codeproject.com/Articles/42382/Creating-a-Numeric-TextBox-Control

    Have fun.


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    • Proposed as answer by Dave A Gordon Wednesday, July 4, 2012 12:31 PM
    Wednesday, July 4, 2012 12:31 PM
  • Check this It will works fine.,..

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                int i = e.KeyChar;
                if (i >= 48 && i <= 57)
                {              
                }
                else
                {
                    MessageBox.Show("You Can Only Enter A Number!");
                    e.Handled = true;
                }
            }


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Wednesday, July 4, 2012 12:32 PM
  • Hi try this code

     char keypress = e.KeyChar;
                if (char.IsDigit(keypress) || e.KeyChar==Convert.ToChar(Keys.Back))
                {
                
    
                }
                else
                {
                    MessageBox.Show("You Can Only Enter A Number!");
                    e.Handled = true;
                }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by san Sanz Wednesday, July 4, 2012 12:34 PM
    • Marked as answer by CoOlDud3 Wednesday, July 4, 2012 2:14 PM
    Wednesday, July 4, 2012 12:34 PM
  • Thank You San Sanz. This is just what i was looking for. Thanks a bunch!

    Best regards

    Wednesday, July 4, 2012 2:15 PM
  • Thank you that would help me as well. But i have too many text boxes already in my application that it would simply take me time to re-add them as masked textboxes. My bad for not adding in the details =). Thanks! =)
    Wednesday, July 4, 2012 2:18 PM
  • thanks

    Tuesday, December 18, 2012 5:28 AM