2009/01/02 | 一个仅允许数字输入的TextBox(继承类)
类别(程序语言) | 评论(0) | 阅读(213) | 发表于 20:55
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;
            }
        }
    }
}
0

评论Comments

日志分类
首页[227]
+随笔[68]
学习札记[67]
科研工具[44]
文献摘译[2]
程序语言[20]
网络技巧[26]
--[0]