如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,"只允许 2 个数字字符 [英] How restrict textbox in C# to only receive numbers and (dot "." or comma ","), after "." or "," only allow 2 number characters

查看:26
本文介绍了如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,"只允许 2 个数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个代码来限制使用 C# 的 TextBox 只允许输入数字 + 逗号(,")或点(.") + 点或逗号后仅 2 个数字所以这样可以看到可能的数字可以进入:

i am trying develop a code to restrict TextBox using C# to only allow numbers entry + comma(",") or dot(".") + only 2 numbers after dot or comma So this way see possible numbers that can entry:

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

了解我的目标吗?我开发了下面的代码

Understood My goal ? I developed code bellow

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

推荐答案

试试这个代码!我希望这有帮助.如果我能进一步帮助您,请告诉我.

Try this code ! I hope this helps. Let me know if I can assist you further.

这是我写的辅助函数

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

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

This your key press event handler

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

                }
            }


        }

这篇关于如何限制 C# 中的文本框只接收数字和(点“."或逗号“,"),在“."之后或“,"只允许 2 个数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆