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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18449388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:14:28  来源:igfitidea点击:

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

c#

提问by Smith

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.

我的目标:我希望文本框接受十进制数字,如 123.45 或 0.45 或 1004.72。如果用户键入 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. How do I make my text box check for decimal numbers? Following is my code:

我的问题:我的代码只检查像 1003 或 567 或 1 这样的数字。它不检查像 123.45 或 0.45 这样的十进制数字。如何让我的文本框检查十进制数字?以下是我的代码:

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. :)

我是新手,提前感谢您的帮助。:)

采纳答案by user2711965

use Decimal.TryParseto 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;
}

回答by arun

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

decimal.Tryparse 对于包含“,”字符的字符串返回真,例如像“0,12”这样的字符串返回真。

回答by KIRAN PRAJAPATI

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