zoukankan      html  css  js  c++  java
  • 限制输入类型

    一、只允许字符输入

     1private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
     2        {
     3            //控制为字符输入
     4            if (this.textBox4.Text.Length == 0)
     5            {
     6                if (Char.IsLetter(e.KeyChar))
     7                    e.Handled = false;
     8                else e.Handled = true;
     9            }

    10            else
    11            {
    12                if (Char.IsLetter(e.KeyChar) || (Keys)e.KeyChar == Keys.Back )
    13                {
    14                    e.Handled = false;
    15                }

    16                else
    17                {
    18                    e.Handled = true;
    19                }

    20            }

    21        }

    22

    二、只允许数字输入
     1private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
     2        {
     3            //控制为数字输入
     4            if (this.textBox5.Text.Length == 0)
     5            {
     6                if (Char.IsDigit(e.KeyChar))
     7                    e.Handled = false;
     8                else e.Handled = true;
     9            }

    10            else
    11            {
    12                if (Char.IsDigit(e.KeyChar) || (Keys)e.KeyChar == Keys.Back || e.KeyChar.ToString() == ".")
    13                {
    14                    e.Handled = false;
    15                }

    16                else
    17                {
    18                    e.Handled = true;
    19                }

    20            }

    21}

    22

    三、判断数字输入(方法二)
     1/// <summary>
     2        /// 判断是否数字
     3        /// </summary>
     4        /// <param name="itemValue"></param>
     5        /// <returns></returns>

     6        private bool IsNumeric(string itemValue)
     7        {
     8            Regex regex = new Regex("^(-?[0-9]*[.]*[0-9]{0,3})$");
     9
    10            return regex.IsMatch(itemValue);
    11        }

    12
    13using System.Text.RegularExpressions;
    14
    15其他情况可以在里面可以找到。char.下面有好多类型。
    16
  • 相关阅读:
    composer阿里云短信服务不支持传参为数值--为2017年短信接口,2018阿里云有更新http://www.cnblogs.com/q1104460935/p/8916096.html
    随机生成字符串,数字,手机号,邮箱
    C#: .net序列化及反序列化 [XmlElement(“节点名称”)] [XmlAttribute(“节点属性”)] (上篇)
    自动升级功能
    C# WinForm 设置按纽为透明,使用背景色
    sql server 2000 单主键高效分页存储过程 (支持多字段排序)
    分页存储过程
    C# WinForm 解决子窗体放大后,子窗体图标放大的问题
    Windows 7/8 64位系统 不能注册32位dll 文件的解决方案
    添加ico图标
  • 原文地址:https://www.cnblogs.com/winnxm/p/911174.html
Copyright © 2011-2022 走看看