如何限制在C#中的文本框只接受数字和点(“”或逗号“”),之后“”或“”只允许2号人物(How re

2019-08-31 09:05发布

我试图开发一个代码使用C#只允许数字条目+逗号来限制文本框(“‘)或句点(’。”)+只有小数点后2个数字或逗号所以这种方式看到可能的数字,可以输入:

3213,04 = OK
3211,664 = Not
32.31 = OK
32.3214 = Not
334,,00 = Not
3247,.00 = Not
214.,00 = Not
32.. = Not
8465,0 = Ok
654.0 = Ok

明白我的目标是什么? 我开发的代码波纹管

private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
{
    if (txtValormetrocubico.TextLength >= 0 && (e.KeyChar == (char)Keys.OemPeriod || e.KeyChar == (char)Keys.Oemcomma))
    {
        //tests 
    }
    else
    {
        if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.' && e.KeyChar != ',')
        {
            e.Handled = true;
        }
        // only allow one decimal point
        if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }

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

Answer 1:

试试这个代码! 我希望这有帮助。 让我知道如果我能进一步协助您。

这是辅助功能我已经写

private bool alreadyExist(string _text , ref char KeyChar)
        {
            if (_text.IndexOf('.')>-1)
            {
                KeyChar = '.';
                return true;
            }
            if (_text.IndexOf(',') > -1)
            {
                KeyChar = ',';
                return true;
            }
            return false;
        }

这是你按键的事件处理程序

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

            //check if '.' , ',' pressed
            char sepratorChar='s';
            if (e.KeyChar == '.' || e.KeyChar == ',')
            {
                // check if it's in the beginning of text not accept
                if (txtValormetrocubico.Text.Length == 0) e.Handled = true;
                // check if it's in the beginning of text not accept
                if (txtValormetrocubico.SelectionStart== 0 ) e.Handled = true;
                // check if there is already exist a '.' , ','
                if (alreadyExist(txtValormetrocubico.Text , ref sepratorChar)) e.Handled = true;
                //check if '.' or ',' is in middle of a number and after it is not a number greater than 99
                if (txtValormetrocubico.SelectionStart != txtValormetrocubico.Text.Length && e.Handled ==false)
                {
                    // '.' or ',' is in the middle
                    string AfterDotString = txtValormetrocubico.Text.Substring(txtValormetrocubico.SelectionStart);

                    if (AfterDotString.Length> 2)
                    {
                        e.Handled = true;
                    }
                }
            }
            //check if a number pressed

            if (Char.IsDigit(e.KeyChar))
            {
                //check if a coma or dot exist
                if (alreadyExist(txtValormetrocubico.Text ,ref sepratorChar))
                {
                    int sepratorPosition = txtValormetrocubico.Text.IndexOf(sepratorChar);
                    string afterSepratorString = txtValormetrocubico.Text.Substring(sepratorPosition + 1 );
                    if (txtValormetrocubico.SelectionStart > sepratorPosition && afterSepratorString.Length >1)
                    {
                        e.Handled = true;
                    }

                }
            }


        }


Answer 2:

我认为你需要像蒙面文本框控件在这里你有一些参考

http://msdn.microsoft.com/en-us/library/kkx4h3az.aspx http://www.c-sharpcorner.com/uploadfile/mahesh/maskedtextbox-in-C-Sharp/

另一种方式做你想要的是使用正则表达式



Answer 3:

那么你可以创建一个通用的函数,并调用它的keypress事件这段代码是一个普遍的实例。

validate_textBox是一个通用功能

private void validate_textBox(TextBox _text, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                    && e.KeyChar != '.' && e.KeyChar != ',')
            {
                e.Handled = true;
            }
            if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.' && e.KeyChar != ',')
            {
                e.Handled = true;
            }

            //check if '.' , ',' pressed
            char sepratorChar = 's';
            if (e.KeyChar == '.' || e.KeyChar == ',')
            {
                // check if it's in the beginning of text not accept
                if (_text.Text.Length == 0) e.Handled = true;
                // check if it's in the beginning of text not accept
                if (_text.SelectionStart == 0) e.Handled = true;
                // check if there is already exist a '.' , ','
                if (alreadyExist(_text.Text, ref sepratorChar)) e.Handled = true;
                //check if '.' or ',' is in middle of a number and after it is not a number greater than 99
                if (_text.SelectionStart != _text.Text.Length && e.Handled == false)
                {
                    // '.' or ',' is in the middle
                    string AfterDotString = _text.Text.Substring(_text.SelectionStart);

                    if (AfterDotString.Length > 2)
                    {
                        e.Handled = true;
                    }
                }
            }
            //check if a number pressed

            if (Char.IsDigit(e.KeyChar))
            {
                //check if a coma or dot exist
                if (alreadyExist(_text.Text, ref sepratorChar))
                {
                    int sepratorPosition = _text.Text.IndexOf(sepratorChar);
                    string afterSepratorString = _text.Text.Substring(sepratorPosition + 1);
                    if (_text.SelectionStart > sepratorPosition && afterSepratorString.Length > 1)
                    {
                        e.Handled = true;
                    }

                }
            }
        } 

然后,你可以调用这样的代码对你的形式让每个文本框

        private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
        {
            validate_textBox(sender as TextBox, e);
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            validate_textBox(sender as TextBox, e);
        }


文章来源: How restrict textbox in C# to only receive numbers and (dot “.” or comma “,”), after “.” or “,” only allow 2 number characters