Numeric text box means, a Text Box which can only accepts numeric values.

Code to implement a numeric text box.

Code should be written on textbox keypress event.

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {           
if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar!='\b')
//checking whether pressed key is number, decimal or backspace.
//If false then we will set Handled variable of KeyPressEventArgs to true.
//By doing this system will get a message that character of keypressed
//is executed and hence aplpa numeric character will not be displayed
//except decimal.
                e.Handled=true;
            else if (e.KeyChar == '.')
                if (textBox1.Text.Contains('.'))
                    e.Handled=true;           
//checking whether decimal key is pressed or not, if true then checking
//whether decimal is already present or not, as we can have only
//one decimal point in a text box.
        }

 

  1. Hi Uttam:It is a great program.  I have modified the code to accommodate '+' character.  It worked fine but then I wanted to allow only one time entry  of '+' .  This part is not working. (It allowed multiple '+' entries). Please help.  Thanks . Rao.

    namespace Samples_Mindstick_NumericTextbox
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {
            }

            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {


                if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar != '\b' && e.KeyChar != '+')

                    //checking whether pressed key is number, decimal backspace  or '+'.

                    //If false then we will set Handled variable of KeyPressEventArgs to true.

                    //By doing this system will get a message that character of keypressed

                    //is executed and hence aplpa numeric character will not be displayed

                    //except decimal.     '+' is added by Rao Inala MD. .

                    e.Handled = true;

                else if (e.KeyChar == '.')

                    if (textBox1.Text.Contains('.'))

                        e.Handled = true;
                    else if (e.KeyChar == '+')

                        if (textBox1.Text.Contains('+'))
                            e.Handled = true;


            //checking whether decimal key is pressed or not, if true then checking

            //whether decimal is already present or not, as we can have only

            //one decimal point in a text box.
            // I add this code to check whether '+' key is pressed or not , if true then checking
                // whether '+' is already present or not .  we can have only one +.
                //My current code is entering more than one' '+ .  How to solve this problem?
            
          }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
           
       }
    }

  1. Hi Rao Inala,
    Replace your code with this code:
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar != '\b' && e.KeyChar != '+')

                    //checking whether pressed key is number, decimal backspace  or '+'.

                    //If false then we will set Handled variable of KeyPressEventArgs to true.

                    //By doing this system will get a message that character of keypressed

                    //is executed and hence aplpa numeric character will not be displayed

                    //except decimal.     '+' is added by Rao Inala MD. .

                    e.Handled = true;

                else if (e.KeyChar == '.')
                {
                    if (textBox1.Text.Contains('.'))

                        e.Handled = true;
                }

                else if (e.KeyChar == '+')

                    if (textBox1.Text.Contains('+'))
                        e.Handled = true;

                //checking whether decimal key is pressed or not, if true then checking

                //whether decimal is already present or not, as we can have only

                //one decimal point in a text box.
                // I add this code to check whether '+' key is pressed or not , if true then checking
                // whether '+' is already present or not .  we can have only one +.
                //My current code is entering more than one' '+ .  How to solve this problem?
            }
    Now it will work as you want.

Leave Comment