zoukankan      html  css  js  c++  java
  • C# 文本输入限制类型,datagridview单元格输入验证

    1.只能输入double类型

      private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e)
            {
                {
                    //数字0~9所对应的keychar为48~57,小数点是46,Backspace是8  
                    e.Handled = true;
                    //输入0-9和Backspace del 有效  
                    if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
                    {
                        e.Handled = false;
                    }
                    if (e.KeyChar == 46)                       //小数点        
                    {
                        if (textBoxX6.Text.Length <= 0)
                            e.Handled = true;           //小数点不能在第一位        
                        else
                        {
                            float f;
                            if (float.TryParse(textBoxX6.Text + e.KeyChar.ToString(), out f))
                            {
                                e.Handled = false;
                            }
                        }
                    }
                }  
    

      2.只能输入数字 

    private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
     {
      // 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22)
      if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 &&
      e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22)
      {
        e.Handled = true;
      }
     }
    

      3.

    ESC  27           7  55  
    SPACE 32        8  56  
    ! 33                 9  57  
    " 34                 :  58  
    # 35                ;  59 
    $ 36                <  60 
    %  37              =  61 
    & 38                >  62 
    ' 39                  ?  63 
    ( 40                 @  64 
    ) 41                 A  65 
    * 42                B  66 
    + 43                C  67 
    '  44                 D  68 
    - 45                 E  69 
    . 46                 F  70 
    / 47                 G  71 
    0  48               H  72 
    1 49                I  73 
    2 50                J  74 
    3  51                  K  75 
    4  52                  L  76 
    5 53                  M  77 
    6  54                  N  78 
    O  79                   g  103 
    P  80                   h  104 
    Q  81                  i  105
    R  82                  j  106
    S  83                  k  107
    T  84                  l  108
    U  85                  m  109
    V  86                  n  110
    W  87                  o  111
    X  88                  p  112
    Y  89                  q  113
    Z  90                  r  114
    [  91                  s  115
      92                  t  116
    ]  93                  u  117
    ^  94                  v  118
    _  95                  w  119
    `  96                  x  120
    a  97                  y  121
    b  98                  z  122
    c  99                  {  123
    d  100                  |  124
    e  101                  }  125
    f  102                  ~  126
    

      

    75=<e.char<=122 为字符

     48=<e.char<=56 为字符

    2.单元格输入验证

       this.dGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dGV_EditingControlShowing);
            }
            void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (this.dGV.CurrentCell.ColumnIndex == 4)
                {
                    e.Control.KeyPress -= new KeyPressEventHandler(TextBoxDec_KeyPress);
                    e.Control.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);
                }
            }
    
            private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.dGV.CurrentCell.ColumnIndex ==4)
                {
                    if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                    {
                        e.Handled = true;
                    }
                }
    
            }
    

      要点:在 EditingControlShowing 的事件中,判断单元格,所在列,调取文本输入事件

  • 相关阅读:
    思科交换机端口安全配置
    华为交换机端口安全配置
    多行文本出现省略号必备的条件(面试题)
    单行文本出现省略号必备的条件(面试题)
    让多个元素在一行显示的方法和技巧(面试题)
    overflow的多个作用
    雪碧图的使用和制作技巧
    列举background属性的8个属性值(面试题)
    background-origin设置背景图像的参考原点(位置)
    background-clip设置对象的背景图像向外裁剪的区域
  • 原文地址:https://www.cnblogs.com/hanke123/p/5753810.html
Copyright © 2011-2022 走看看