articles

home / developersection / articles / how to create numeric text box in c sharp

How to create Numeric Text Box in C sharp

Uttam Misra 15121 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.

  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.
        }

 


c# c# 
Updated 04-Mar-2020
Uttam Misra

Information Technology

More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

2 Comments

Comments

Liked By