文本框应仅获取数字且仅包含小数点后一位 [英] Textbox should get only numeric with only one decimal

查看:43
本文介绍了文本框应仅获取数字且仅包含小数点后一位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的编码中使用.但它也接受字母.如何防止这种情况,
[我给定keypreview = false并尝试使用keypreview = true]

i used below coding. but its accepting alphabets also., how to prevent this.,
[i given keypreview=false and tried with keypreview=true]

private void txtorate_KeyPress(object sender,KeyPressEventArgs e)
        {

            if(!char.IsControl(e.KeyChar)&&(!char.IsDigit(e.KeyChar))&& e.KeyChar!='.')
            {
                e.Handled = true;
            }

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

推荐答案

为什么不使用 ^ ]代替?几乎就是它的设计目标.
Why not use a NumericUpDown[^] instead? That is pretty much what it was designed for...


请参阅此MSDN文章,其中向您显示代码:
http://msdn.microsoft.com/en-us/library/ms229644%28v = vs.80%29.aspx [ ^ ]
See this MSDN article which shows you the code:
http://msdn.microsoft.com/en-us/library/ms229644%28v=vs.80%29.aspx[^]


使用||代替&&在第一个条件下:

Use || instead of && in first condition:

private void txtorate_KeyPress(object sender,KeyPressEventArgs e)
{
    if(!char.Control(e.KeyChar) || (!char.IsDigit(e.KeyChar)) || e.KeyChar != '.')

        if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)

                e.Handled = true;
}


我认为这会有所帮助.


I think it would help.


这篇关于文本框应仅获取数字且仅包含小数点后一位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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