十进制TextBox,例如1.54 [英] Decimal TextBox that get for example 1.54

查看:88
本文介绍了十进制TextBox,例如1.54的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i有一个textBox而不是写在Event OnKeyPress下面代码



hello
i have a textBox than write in Event OnKeyPress below code

protected override void OnKeyPress(KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
    base.OnKeyPress(e);
}





它只能得到整数

它不会得到。对于十进制数,例如5.3

如何为我的目的更改上面的代码? :):)



it only get integer
it don''t get "." for decimal number for example 5.3
how i can change above code for my purpose? :):)

推荐答案

如果我理解你的问题,要获得小数分隔符,你可以使用NumberFormatInfo.NumberDecimalSeparator Property [ ^ ]
If I understood your question correctly, to get the decimal separator you can use NumberFormatInfo.NumberDecimalSeparator Property[^]


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


hi,








private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (!(e.KeyChar >= '0' && e.KeyChar <= '9' ||  e.KeyChar =='.'))

{
count=0;
if(!e.keychar=='.')
{
    count++;
    if(count>1)
        e.Handled=false;
}
else
e.Handled = true;
}
}


这篇关于十进制TextBox,例如1.54的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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