NumericUpDown 控件输入限制小数位
生活随笔
收集整理的這篇文章主要介紹了
NumericUpDown 控件输入限制小数位
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在做項目的時候,需要提供給用戶只能輸入2為小數的功能。只允許用戶輸入數字的情況,自然用控件NumericUpDown,同時,該控件有個屬性NumericUpDown.DecimalPlaces,該屬性為:數字顯示框中要顯示的十進制位數。默認值為 0。單獨靠這個屬性,顯示輸入2為小數,可以。但是Value還是輸入的數字。嗯,用Decimal.Round();Decimal.Floor(),再或者是截取字符串,覺得都不合適。我是這么做的:
代碼?1??private?void?numericUpDown1_KeyPress(object?sender,?KeyPressEventArgs?e)?2?????????{
?3?????????????if?(((NumericUpDown)sender).Text.Trim().IndexOf('.')?>?-1)
?4?????????????{
?5?????????????????if?(((NumericUpDown)sender).Text.Trim().Substring(((NumericUpDown)sender).Text.Trim().IndexOf('.')?+?1).Length?>=?2)//定義小數位
?6?????????????????{
?7?????????????????????if?(!char.IsDigit(e.KeyChar)?)
?8?????????????????????{
?9?????????????????????????e.Handled?=?false;
10?????????????????????}
11?????????????????????else
12?????????????????????{
13?????????????????????????e.Handled?=?true;
14?????????????????????}
15?????????????????}
16?????????????}
17?????????}
?
?
思路就是:限制輸入。完事兒。?
?
轉載于:https://www.cnblogs.com/angleSJW/archive/2010/10/25/1860596.html
總結
以上是生活随笔為你收集整理的NumericUpDown 控件输入限制小数位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle安装配置手册
- 下一篇: 嵌入式Linux驱动程序