4

My Goal: I want textbox to accept the decimal numbers like 123.45 or 0.45 or 1004.72. If the user types in letters like a or b or c, the program should display a message alerting the user to input only numbers.

My Problem: My code only checks for numbers like 1003 or 567 or 1. It does not check for decimal numbers like 123.45 or 0.45. How do I make my text box check for decimal numbers? Following is my code:

namespace Error_Testing
{

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

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string tString = textBox1.Text;
            if (tString.Trim() == "") return;
            for (int i = 0; i < tString.Length; i++)
            {
                if (!char.IsNumber(tString[i]))
                {
                    MessageBox.Show("Please enter a valid number");
                    return;
                }
            }
            //If it get's here it's a valid number
        }
    } 
}

I am a newbie and Thanks for your help in advance. :)

6
  • For one.. you aren't handling the '.' character. What happens when they enter this into the textbox? Aug 26, 2013 at 17:14
  • Are you trying to write you own check or are you OK with using a built in one? This is trivial in .NET with decimal.TryParse or double.TryParse.
    – cadrell0
    Aug 26, 2013 at 17:17
  • @JonLaMarr: If I enter 123. in textbox, a dialogue box appears on the screen: "Please enter a valid number". This means that the textbox is not checking for decimal numbers.
    – Smith
    Aug 26, 2013 at 17:17
  • @cadrell0: Yes, I am okay with the built in one or whichever is easier. However, I don't know how to do it since this is one of my first few attempts at c# coding.
    – Smith
    Aug 26, 2013 at 17:18
  • 1
    @Smith NewHire's answer is as easy as it gets.
    – cadrell0
    Aug 26, 2013 at 17:19

3 Answers 3

23

use Decimal.TryParse to check if the string entered is decimal or not.

decimal d;
if(decimal.TryParse(textBox1.Text, out d))
{
    //valid 
}
else
{
    //invalid
    MessageBox.Show("Please enter a valid number");
    return;
}
3
  • For integers, use int.TryParse() Aug 26, 2013 at 17:17
  • @NewHire: It works! Thanks for helping. I will mark is as Answer soon. Thanks again.
    – Smith
    Aug 26, 2013 at 17:21
  • @Smith, you are welcome, For parsing any number should always look for TryParse family methods, provided with different types. e.g. int.TryParse, DateTime.TryParse etc. Aug 26, 2013 at 17:22
0

decimal.Tryparse returns true for string containing "," character and for example string like "0,12" returns true.

1
  • Some countries use the "." as decimal separator and other countries use the "," as decimal separator. You can change this by changing your culture. See this answer for more information stackoverflow answer
    – Erik
    Mar 29, 2017 at 15:42
0
private void txtrate_TextChanged_1(object sender, EventArgs e)
        {
            double parsedValue;
            decimal d;
            // That Check the Value Double or Not
            if (!double.TryParse(txtrate.Text, out parsedValue))
            {
                //Then Check The Value Decimal or double Becouse The Retailler Software Tack A decimal or double value
                if (decimal.TryParse(txtrate.Text, out d) || double.TryParse(txtrate.Text, out parsedValue))
                {
                    purchase();
                }
                else
                {
                    //otherwise focus on agin TextBox With Value 0
                    txtrate.Focus();                  
                    txtrate.Text = "0";                   
                }


            }
            else
            {
                // that function will be used for calculation Like 
                purchase();
                /*  if (txtqty.Text != "" && txtrate.Text != "")
                  {
                      double rate = Convert.ToDouble(txtrate.Text);
                      double Qty = Convert.ToDouble(txtqty.Text);
                      amt = rate * Qty;
                  }*/

            }`enter code here`
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.