• 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 的事件中,判断单元格,所在列,调取文本输入事件

  • 相关阅读:
    使用VS进入源码调试
    Nlog配置
    一个极简的爬虫
    简单的调用图灵机器人
    docker部署netcore项目 nginx负载均衡
    windows nginx负载均衡
    windows服务器环境配置redis sentinel部署
    ASP.NET资源大全-知识分享
    ABP动态生成WebAPI
    windows服务器环境下安装redis
  • 原文地址:https://www.cnblogs.com/hanke123/p/5753810.html
Copyright © 2020-2023  润新知