如何在c#中使用Handel小数位 [英] How to Handel decimal places in c#
本文介绍了如何在c#中使用Handel小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的问题
在ac#win应用程序中它包含一个名为txtQty的文本框,它包含使用以下代码输入字符的块
if (!char.IsControl(e.KeyChar)&& ;!char.IsDigit(e.KeyChar)&& e.KeyChar!= ' 。' )
{
e.Handled = true ;
}
但是用户可以输入这样的值(5 ...)这个输入生成错误如何阻止这个
谢谢
解决方案
请查看以下内容:
使用MaskedTextBox控件 [ ^ ]
蒙面C#TextBox控件 [ ^ ]
亲切问候,
您还可以在代码中检查double.tryparse,因为您想接受一位小数。
if (!char.IsControl (e.KeyChar)&& !char.IsDigit(e.KeyChar)&& e.KeyChar!= ' 。')
{
double dblValue;
e.Handled = double .tryparse(txtQty.Text, out dblValue);
}
这可以确保文本框中的值是一个有效的十进制数,并保护免受额外的....(点:))
I have question like this
in a c# win application it''s contain text box called txtQty it has block to enter characters by using following code
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
but user can enter value like this (5...) this input generate an error how to block this
thanks
解决方案
Hi,
Please have a look at the following:
Working with the MaskedTextBox Control[^]
Masked C# TextBox Control[^]
Kind regards,
You can also check for double.tryparse inside your code since you want to accept one decimal place.
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { double dblValue; e.Handled = double.tryparse(txtQty.Text,out dblValue); }
this ensures that the value entred in the text box is a valid decimal number and protects from additional ....( dots :))
这篇关于如何在c#中使用Handel小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文