articles

Home / DeveloperSection / Articles / How to create Numeric Text Box in C sharp

How to create Numeric Text Box in C sharp

Uttam Misra14746 31-Jul-2010

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

How to create Numeric Text Box in C sharp

Code to implement a numeric text box.

Code should be written on textbox keypress event.

  privatevoid 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;
            elseif (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.
        }

 


Updated 04-Mar-2020
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By