textBox编号格式为0000.00 [英] textBox number formating to 0000.00

查看:82
本文介绍了textBox编号格式为0000.00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望表单textBox2可以只键入以下格式的值:

I want that to forms textBox2 would be possible to type only values in such format:

000.00
87979845.00



我的意思是无限数字,然后是十进制/点,后面只有两个数字。到目前为止,除了检查点后面有多少个数字之外,我还有其他所有内容......


I mean unlimited digit number, then decimal/dot and only two numbers after it. So far I have everything except check of how many numbers after the dot is...

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && !textBox2.Text.Contains(".") || e.KeyChar == '\b')
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}



如何在此代码中实现?


How I could implement that in this code?

推荐答案

修改你的功能如下......

Modify your function like below...
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && !textBox2.Text.Contains(".") || e.KeyChar == '\b')
    {
        if (textBox2.Text.Contains("."))
        {
            string[] txt = textBox2.Text.Split('.');

            if (txt[1].Length > 1)
            {
                if (e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = true;
                }
            }
        }
        else
        {
            e.Handled = false;
        }
    }
    else
    {
        e.Handled = true;
    }
}



让我知道它是否有效......


Let me know if it works or not...


你没有提供足够的信息......一些国家使用不同的十进制值格式,例如:

英语: 100.23

波兰语: 100,23



你知道我的意思吗,现在?



请参考以下链接:

http://msdn.microsoft.com/en-us/library/aa292205%28v=vs.71%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo。 aspx [ ^ ]



我认为您正在寻找数字验证器。请参阅过去的答案:仅允许数字字符进入文本框 [ ^ ]



但是如果你想为TextBox添加新功能并将其用作MaskedTextBox,请参考以下内容:

wpf中的ContentStringFormat [ ^ ]

如何在文本框中验证用户输入的字符串? [ ^ ]

将掩盖的输入显示到文本框中 [ ^ ]



CP文章:

数字文本框:允许用户输入数字数据简单的方法 [ ^ ]
You did not provide enough information... Some countries uses different format for decimal values, for example:
English: 100.23
Polish: 100,23

Do you know what i mean, now?

Please, refer below links:
http://msdn.microsoft.com/en-us/library/aa292205%28v=vs.71%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx[^]

I think you are looking for numeric validator. Please, see past answer: allow Only numeric Characters into Textbox[^]

But if you want to add new functionality to TextBox and use it as MaskedTextBox, please, refer these:
ContentStringFormat in wpf[^]
How can I validate user entered string in textbox?[^]
showing masked nput into Textbox[^]

CP article:
Numeric TextBox : Allow your users to enter numeric data the easy way[^]


这篇关于textBox编号格式为0000.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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