zoukankan      html  css  js  c++  java
  • c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数

     1        #region 设置数量等textbox控件样式及限制条件(具体调用的方法就是重写或直接调用ShieldNumberTextBoxOtherKeys函数)
     2 
     3         /// <summary>
     4         /// 屏蔽数字textbox的其他字符串
     5         /// </summary>
     6         /// <param name="sender"></param>
     7         /// <param name="e"></param>
     8         public virtual void tBoxNumbers_KeyDown(object sender, KeyEventArgs e)
     9         {
    10             e.SuppressKeyPress = true;
    11 
    12             switch (e.KeyCode)
    13             {
    14                 case Keys.D0:
    15                 case Keys.D1:
    16                 case Keys.D2:
    17                 case Keys.D3:
    18                 case Keys.D4:
    19                 case Keys.D5:
    20                 case Keys.D6:
    21                 case Keys.D7:
    22                 case Keys.D8:
    23                 case Keys.D9:
    24                 case Keys.NumPad0:
    25                 case Keys.NumPad1:
    26                 case Keys.NumPad2:
    27                 case Keys.NumPad3:
    28                 case Keys.NumPad4:
    29                 case Keys.NumPad5:
    30                 case Keys.NumPad6:
    31                 case Keys.NumPad7:
    32                 case Keys.NumPad8:
    33                 case Keys.NumPad9:
    34                 case Keys.Back:
    35                 case Keys.OemPeriod:
    36                 case Keys.Delete:
    37                 case Keys.Decimal:
    38                     e.SuppressKeyPress = false;
    39                     break;
    40                 default:
    41                     break;
    42             }
    43         }
    44 
    45         public virtual void tBoxNumbers_KeyPress(object sender, KeyPressEventArgs e)
    46         {
    47             TextBox tBox = sender as TextBox;
    48             char c = e.KeyChar;
    49 
    50             if (c.ToString().Equals("."))
    51             {
    52                 if (tBox.Text.Length <= 0)
    53                     e.Handled = true;           //小数点不能在第一位     
    54                 else
    55                 {
    56                     float f;
    57                     float oldf;
    58                     bool b1 = false, b2 = false;
    59                     b1 = float.TryParse(tBox.Text, out oldf);
    60                     b2 = float.TryParse(tBox.Text + e.KeyChar.ToString(), out f);
    61                     if (b2 == false)
    62                     {
    63                         if (b1 == true)
    64                             e.Handled = true;
    65                         else
    66                             e.Handled = false;
    67                     }
    68                 }
    69             }
    70         }
    71 
    72         /// <summary>
    73         /// 屏蔽数字textbox的其他字符串
    74         /// </summary>
    75         /// <param name="tbox">要屏蔽的textbox</param>
    76         public virtual void ShieldNumberTextBoxOtherKeys(TextBox tbox)
    77         {
    78             tbox.ImeMode = ImeMode.Disable;
    79             tbox.KeyDown += tBoxNumbers_KeyDown;
    80             tbox.KeyPress += tBoxNumbers_KeyPress;
    81         }
    82 
    83         #endregion
    人生,总是有一些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。
  • 相关阅读:
    error_reporting(“E_ALL”)和ini_set(“display_errors”, “on”)的区别?
    linux命令awk的详解
    Ubuntu 能PING IP但不能PING主机域名的解决方法
    从github checkout子文件夹
    zuul简单使用
    docker for windows 10 添加阿里云镜像仓库无效问题
    Spring Boot 进行Bean Validate和Method Validate
    JVM调优-GC参数
    Spring Aop: 关于继承和execution target this @annotation
    ReentrantLock原理
  • 原文地址:https://www.cnblogs.com/yuzhou133/p/4112539.html
Copyright © 2011-2022 走看看