-1

How to make a textbox that only accepts number and decimal points in c# ?

looking for something very simple if anyone knows please help !

Abz
  • 29
  • 1
  • 4

2 Answers2

0

try this code...

 if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            { e.Handled = true; }
            TextBox txtDecimal = sender as TextBox;
            if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
            {
                e.Handled = true;
            }
0

I think you are looking for a Masked TextBox. Create one, and put this Regex into the Mask section: ^(\d+)?+([\.]{1})?+([\d]{1,2})?$

Ian H.
  • 3,840
  • 4
  • 30
  • 60