一个仅允许数字输入的TextBox(继承类)

      程序语言 2009-1-2 20:55:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows .Forms ;
using System.Text .RegularExpressions ;
using System.Globalization;
 
/* Updated at Jan 1st, 2009 By 水南(skyline-moon.blog.5d.cn)
 * This code show you how to write a TextBox that allow only numeric data can be input
 * original code can be found in : http://msdn.microsoft.com/en-us/library/ms229644.aspx
 * License GNU/GPL
 *
*/
 
namespace yourNameSpce-
{
    public class NumTxtBox : TextBox
    {
        private bool isNumeric(string str)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$ ");
            return regex.IsMatch(str);
 
        }
 
        bool allowSpace = false;
 
        // Restricts the entry of characters to digits (including hex), the negative sign,
        // the decimal point, and editing keystrokes (backspace).
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
 
            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            //string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;
            string positiveSign = numberFormatInfo.PositiveSign;
              
 
            string keyInput = e.KeyChar.ToString();
 
            //if (groupSeparator == ((char)160).ToString()) groupSeparator = " "; // workaround, change non-breaking space to normal space
 
            if (Char.IsDigit(e.KeyChar) )
            {
                // Digits are OK
            }
            else if ((e.KeyChar == 'e' || e.KeyChar == 'E') && isNumeric(this.Text))
           {
                //2009-1-2(zenghuasen):Scientific(exponential) format data will be accepted here 
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(positiveSign) ||
             keyInput.Equals(negativeSign))
            {
                // Decimal separator is OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            //    {
            //     // Let the edit control handle control and alt key combinations
            //    }
            else if (this.allowSpace && e.KeyChar == ' ')
            {
 
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }
 
        public int IntValue
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }
 
        public decimal DecimalValue
        {
            get
            {
                return Decimal.Parse(this.Text);
            }
        }
 
        public bool AllowSpace
        {
            set
            {
                this.allowSpace = value;
            }
 
            get
            {
                return this.allowSpace;
            }
        }
    }
}
标签集:TAGS:C#
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}