c# 控制TextBox只能输入小数(只能输入一个小数点,小数点后只能输入两位,第一位不能是小数点)

 

 

这个有一些问题

 

先输入整数部分后再把整数部分删除,变相地让小数点跑到第一位

在两位小数的情况下如果选中两位小数也无法更改

 

网友 ymwcwee 已更正 请大家参考他的代码 

<p><textarea cols="50" rows="15" name="code" class="c-sharp">/// &lt;summary&gt;

http://blog.csdn.net/jzz870519/archive/2010/08/08/5796876.aspx,因为我在他上面回复代码总是失败,所以自己开了。。。

 

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  2. {  
  3.     if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete)  
  4.     {  
  5.         e.Handled = false;          //让操作生效     
  6.         int j = 0;                  //记录小数点个数  
  7.         int k = 0;                  //记录小数位数  
  8.         int dotloc = -1;            //记录小数点位置  
  9.         bool flag = false;          //如果有小数点就让flag值为true  
  10.         //  
  11.         //去除首位是0的判断,因为我们不知道用户的意图,或许ta下次要在小数点前面输入数字。  
  12.         /* 
  13.         if (textBox1.Text.Length == 0) 
  14.         { 
  15.             if (e.KeyChar == '.') 
  16.             { 
  17.                 e.Handled = true; 
  18.             } 
  19.         } 
  20.         */  
  21.         //  
  22.         for (int i = 0; i < textBox1.Text.Length; i++)  
  23.         {  
  24.             if (textBox1.Text[i] == '.')  
  25.             {  
  26.                 j++;  
  27.                 flag = true;  
  28.                 dotloc = i;  
  29.             }  
  30.   
  31.             if (flag)  
  32.             {  
  33.                 if (char.IsNumber(textBox1.Text[i]) && e.KeyChar != (char)Keys.Back && e.KeyChar != (char)Keys.Delete)  
  34.                 {  
  35.                     k++;  
  36.                 }  
  37.             }  
  38.   
  39.             if (j >= 1)  
  40.             {  
  41.                 if (e.KeyChar == '.')  
  42.                 {  
  43.                     if (textBox1.SelectedText.IndexOf('.') == -1)  
  44.                         e.Handled = true;             //输入“.”,选取部分没有“.”操作失效  
  45.                 }  
  46.             }  
  47.   
  48.             if (!flag)                  //此处控制没有小数点时添加小数点是否满足两位小数的情况  
  49.             {  
  50.                 if (e.KeyChar == '.')  
  51.                 {  
  52.                     if (textBox1.Text.Length - textBox1.SelectionStart - textBox1.SelectedText.Length > 2)        //the condition also can be instead of "textBox1.Text.Substring(textBox1.SelectionStart).Length-textBox1.SelectionLength>2"   
  53.                         e.Handled = true;  
  54.                 }  
  55.             }  
  56.   
  57.             if (k == 2)  
  58.             {  
  59.                 if (textBox1.SelectionStart > textBox1.Text.IndexOf('.') && textBox1.SelectedText.Length == 0 && e.KeyChar!  
  60.   
  61. =(char)Keys.Delete && e.KeyChar!=(char)Keys.Back)      //如果已经有两位小数,光标在小数点右边,  
  62.                     e.Handled = true;  
  63.             }  
  64.         }  
  65.     }  
  66.     else  
  67.     {  
  68.         e.Handled = true;  
  69.     }   
  70. }  

 

</textarea></p>

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值