C# “允许一个”的文本框验证&引用;c值#

C# “允许一个”的文本框验证&引用;c值#,c#,.net,winforms,textbox,C#,.net,Winforms,Textbox,我希望文本框验证只允许一个值和数字。意味着我的文本框值应该只包含数字和一个值。值应该类似于123.50。 我正在使用代码在我的值末尾添加.oo或.50值。 我的代码是 double x; double.TryParse(tb.Text, out x); tb.Text = x.ToString(".00"); 它从键盘上取下所有键,但我只想取数字和一个值。为文本框添加一个事件处理程序 private void textBox1_KeyPress(object sender, KeyPressE

我希望文本框验证只允许一个
值和数字。意味着我的文本框值应该只包含数字和一个
值。值应该类似于123.50。 我正在使用代码在我的值末尾添加
.oo
.50
值。 我的代码是

double x;
double.TryParse(tb.Text, out x);
tb.Text = x.ToString(".00");
它从键盘上取下所有键,但我只想取数字和一个
值。

为文本框添加一个事件处理程序

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar))  //bypass control keys
    {
        int dotIndex = textBox1.Text.IndexOf('.');
        if (char.IsDigit(e.KeyChar))     //ensure it's a digit
        {   //we cannot accept another digit if
            if (dotIndex != -1 &&  //there is already a dot and
                //dot is to the left from the cursor position and
                dotIndex < textBox1.SelectionStart &&
                //there're already 2 symbols to the right from the dot
                textBox1.Text.Substring(dotIndex + 1).Length >= 2)
            {
                e.Handled = true;
            }
        }
        else //we cannot accept this char if
            e.Handled = e.KeyChar != '.' || //it's not a dot or
                        //there is already a dot in the text or
                        dotIndex != -1 ||   
                        //text is empty or
                        textBox1.Text.Length == 0 || 
                        //there are more than 2 symbols from cursor position
                        //to the end of the text
                        textBox1.SelectionStart + 2 < textBox1.Text.Length;
    }
}
我还添加了一些检查,以确保您不仅可以在文本末尾插入数字,还可以在任何位置插入数字。点也一样。它控制从圆点向右的数字不超过2位。我曾经在文本框中获取光标的位置。检查此线程以了解更多信息:

为文本框添加事件处理程序

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar))  //bypass control keys
    {
        int dotIndex = textBox1.Text.IndexOf('.');
        if (char.IsDigit(e.KeyChar))     //ensure it's a digit
        {   //we cannot accept another digit if
            if (dotIndex != -1 &&  //there is already a dot and
                //dot is to the left from the cursor position and
                dotIndex < textBox1.SelectionStart &&
                //there're already 2 symbols to the right from the dot
                textBox1.Text.Substring(dotIndex + 1).Length >= 2)
            {
                e.Handled = true;
            }
        }
        else //we cannot accept this char if
            e.Handled = e.KeyChar != '.' || //it's not a dot or
                        //there is already a dot in the text or
                        dotIndex != -1 ||   
                        //text is empty or
                        textBox1.Text.Length == 0 || 
                        //there are more than 2 symbols from cursor position
                        //to the end of the text
                        textBox1.SelectionStart + 2 < textBox1.Text.Length;
    }
}
我还添加了一些检查,以确保您不仅可以在文本末尾插入数字,还可以在任何位置插入数字。点也一样。它控制从圆点向右的数字不超过2位。我曾经在文本框中获取光标的位置。检查此线程以了解更多信息:

尝试此线程

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
               && !char.IsDigit(e.KeyChar)
               && e.KeyChar != '.')
            e.Handled = true;

        // only allow one decimal point
        if (e.KeyChar == '.'
            && textBox1.Text.IndexOf('.') > -1)
            e.Handled = true;
    }
试试这个

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
               && !char.IsDigit(e.KeyChar)
               && e.KeyChar != '.')
            e.Handled = true;

        // only allow one decimal point
        if (e.KeyChar == '.'
            && textBox1.Text.IndexOf('.') > -1)
            e.Handled = true;
    }

尝试此代码,只需替换所需的输入类型“validinpu”字符串

try
{
    short charCode = (short)Strings.Asc(e.KeyChar);
    string validinput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789 .";
    if (Strings.InStr(validamt, Conversions.ToString(Strings.Chr(charCode)), Microsoft.VisualBasic.CompareMethod.Binary) == 0)
    {
        charCode = 0;
    }
    if (charCode == 0)
    {
        e.Handled = true;
    }
}

尝试此代码,只需替换所需的输入类型“validinpu”字符串

try
{
    short charCode = (short)Strings.Asc(e.KeyChar);
    string validinput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789 .";
    if (Strings.InStr(validamt, Conversions.ToString(Strings.Chr(charCode)), Microsoft.VisualBasic.CompareMethod.Binary) == 0)
    {
        charCode = 0;
    }
    if (charCode == 0)
    {
        e.Handled = true;
    }
}

另一个例子,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
    {
         // To disallow typing in the beginning writing
        if (txtPrice.Text.Length == 0)
        {
            if (e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }

另一个例子,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
    {
         // To disallow typing in the beginning writing
        if (txtPrice.Text.Length == 0)
        {
            if (e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }

简单地说,在文本框的按键事件中,您可以这样做

        e.Handled = !char.IsDigit(e.KeyChar)&&(e.KeyChar != '.') && !char.IsControl(e.KeyChar);
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        {
            e.Handled = true;
        }

简单地说,在文本框的按键事件中,您可以这样做

        e.Handled = !char.IsDigit(e.KeyChar)&&(e.KeyChar != '.') && !char.IsControl(e.KeyChar);
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        {
            e.Handled = true;
        }

也试试这个短的

e.Handled = (!(e.KeyChar == (char)Keys.Back || e.KeyChar == '.')); //allow  dot and Backspace
e.Handled = (e.KeyChar == '.' && TextBox1.Text.Contains(".")); //allow only one dot

此示例只允许一个退格

也可以尝试此短点

e.Handled = (!(e.KeyChar == (char)Keys.Back || e.KeyChar == '.')); //allow  dot and Backspace
e.Handled = (e.KeyChar == '.' && TextBox1.Text.Contains(".")); //allow only one dot
 if (textBox.Text!="")
        {
            string txt = textBox.Text;
            if (e.KeyChar.ToString().Any(Char.IsNumber) || e.KeyChar == '.')
            {
                textBox.Text = rate;
            }
            else
            {
                MessageBox.Show("Number Only", "Warning");
                textBox.Text = "";
            }
        }
此示例仅允许一个点和退格

 if (textBox.Text!="")
        {
            string txt = textBox.Text;
            if (e.KeyChar.ToString().Any(Char.IsNumber) || e.KeyChar == '.')
            {
                textBox.Text = rate;
            }
            else
            {
                MessageBox.Show("Number Only", "Warning");
                textBox.Text = "";
            }
        }
我的测试代码


我的测试代码

只是一个想法,你考虑过使用NumericUpDown控件吗?没有。。。应该是textboxKonstantin给出了正确的答案,但我想在这里指出一件事——像这样把它放回textboxKonstantin可以让你进入一个循环,因为它改变了文本,导致它再次验证。此外,您正在丢弃TryParse的结果--如果它无效,它将使缓冲区归零,而不是拒绝有问题的键。只是想一想,您是否考虑过使用NumericUpDown控件?不。。。应该是textboxKonstantin给出了正确的答案,但我想在这里指出一件事——像这样把它放回textboxKonstantin可以让你进入一个循环,因为它改变了文本,导致它再次验证。另外,您正在丢弃TryParse的结果--如果它无效,它将使缓冲区归零,而不是拒绝有问题的密钥。非常感谢。。它正在工作,先生。。但后台空间不起作用。我能做什么?如果(e.KeyChar.Equals('\b')){e.Handled=false;}否则{//您剩余的代码}非常感谢。。它正在工作,先生。。但后台空间不起作用。我能做什么?如果(e.KeyChar.Equals('\b')){e.Handled=false;}或者{//您的剩余代码},请尝试此操作