码迷,mamicode.com
首页 > 其他好文 > 详细

16 如何使一个文本框只能接受数字

时间:2014-06-24 21:45:43      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   tar   ext   com   

(1) 使用NumericUpDown
(2) 处理KeyPress 按钮

(3) 处理Text_Changed 事件


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;



namespace MVA20QuestionWin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       private void txtNumeric_TextChanged(object sender, EventArgs e)
        {
        //    if (!Regex.IsMatch("[0-9]", txtNumeric.Text))
        //    {
        //        MessageBox.Show("Please enter only numbers.");
        //        txtNumeric.Text.Remove(txtNumeric.Text.Length - 1);
        //    }
        }

        private void txtNumeric_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar); //&& !char.IsControl(e.KeyChar);
        }

    }
}


16 如何使一个文本框只能接受数字,布布扣,bubuko.com

16 如何使一个文本框只能接受数字

标签:class   blog   code   tar   ext   com   

原文地址:http://blog.csdn.net/u011685627/article/details/33471549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!