简体   繁体   English

检查TextBox输入是否为十进制数字-C#

[英]Check if TextBox input is a decimal number or not - C#

My Goal: I want textbox to accept the decimal numbers like 123.45 or 0.45 or 1004.72. 我的目标:我希望文本框接受十进制数字,例如123.45或0.45或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. 如果用户键入字母,例如a或b或c,程序将显示一条消息,提醒用户仅输入数字。

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. 我的问题:我的代码仅检查数字1003或567或1。它不检查十进制数字123.45或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. 我是新手,感谢您的提前帮助。 :) :)

use Decimal.TryParse to check if the string entered is decimal or not. 使用Decimal.TryParse来检查输入的字符串是否为十进制。

decimal d;
if(decimal.TryParse(textBox1.Text, out d))
{
    //valid 
}
else
{
    //invalid
    MessageBox.Show("Please enter a valid number");
    return;
}

decimal.Tryparse对于包含“,”字符的字符串返回true,例如,类似“ 0,12”的字符串返回true。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM