zoukankan      html  css  js  c++  java
  • WINFORM限制textbox只能输入数字[转]

    首先引用

    using System.Text.RegularExpressions;

    给TextBox添加KeyPress事件,代码如下:

     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
         
    if ((Convert.ToInt32(e.KeyChar) == 8))
        
    {
              e.Handled
    = false;
        }

        
    else
        {
              Regex numRegex
    = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
              Match Result
    = numRegex.Match(Convert.ToString(e.KeyChar));
             
    if (Result.Success)
             {
                    e.Handled
    = false;
             }

             
    else

             {
                    e.Handled
    = true;
             }

        }
    }

    此时还是不完全的,因为这时候单击右键的时候可以将非数字文字粘贴到textbox中去,在vs2005中我们只要这样设置就可以:

    this.textBox1.ShortcutsEnabled = false;


  • 相关阅读:
    MQTT
    群晖搭建webssh
    OSI 协议
    centos7 yum安装ffmpeg,以及ffmpeg的简单用法
    centos7 RTMP直播服务器搭建
    elasticsearch
    H5的storage
    bootstrap 列表組
    eclipse的debug模式下启动不了tomcat
    bootstrap collapse
  • 原文地址:https://www.cnblogs.com/mane/p/2036078.html
Copyright © 2011-2022 走看看