how to allow textbox to acccept only numbers and decimal point?

i want to enter price value in my textbox.i want it to accept only numbers and one decimal point.right now its taking only numbers but not decimal point.the code i hav written is given below bt its only to accept numbers not decimal point.
 string s = txtPrice.Text;
            int chars = s.Length;
           // char p = '.';
            for (int i = 0; i < chars; i++)
            {
                if (!char.IsNumber(s, i))
                {                
                        MessageBox.Show("Enter only numbers");
                        txtPrice.Text = s.Remove(i, 1);
                        txtPrice.Select(txtPrice.Text.Length, 0);
                        return;                  
                }
Fahed_Ahmed_ShaikAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Bob LearnedConnect With a Mentor Commented:
How about something like this?

if (!char.IsNumber(s, i) || s[i] != '.')
0
 
60MXGConnect With a Mentor Commented:
if (!char.IsNumber(s, i))
maybe try

 if (!char.IsDigit(s, i))
????
0
 
birbilisCommented:
try using a masked edit box WinForms control
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.