关于textbox或combobox限制输入内容

来源:互联网 发布:2017年海关数据 编辑:程序博客网 时间:2024/06/07 08:04
private void comboBox3_KeyPress( object sender, KeyPressEventArgs e )        {            //e.KeyChar < 48 || e.KeyChar > 57表示0-9的数字            //e.KeyChar != 46 表示小数点            //e.KeyChar != 8 表示退格键            //e.KeyChar != 3  e.KeyChar != 22 表示复制、粘贴            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 46) && e.KeyChar != 8)            {                MessageBox.Show( "请输入数字" );                e.Handled = true;              }else if (char.IsPunctuation(e.KeyChar))            {                if (comboBox3.Text.LastIndexOf( '.' ) != -1)                {                    e.Handled = true;                    MessageBox.Show( "只能输入一个小数点" );                }            }        }private void comboBox3_TextChanged( object sender, EventArgs e )        {            //若小数点在第一位,则在前面加0            if (comboBox3.Text.Substring( 0 ) == ".")            {                this.comboBox3.Text = "0.";                comboBox3.SelectionStart = 2;            }        }private void textBox5_KeyPress( object sender, KeyPressEventArgs e )        {            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)            {                e.Handled = true;                MessageBox.Show( "只允许输入整数" );            }        }正则表达式验证decimal(n,m)数据类型:1、通过写类,textbox或者combobox调用验证 /// <summary>        /// 匹配decimal(n,m)        /// </summary>        /// <param name="tb"></param>        /// <returns></returns>        public static bool nmDecimal( TextBox tb )        {            bool res = Regex.IsMatch( tb.Text, @"^\d{1,n-m}(?:\.\d{1,m})?$" );            return res;        } 调用方法:private void textBox27_LostFocus( object sender, EventArgs e )        {             //DateDay类名称             if (!DateDay.nmDecimal( textBox27 ))            {                this.textBox27.Text = "";                MessageBox.Show( "只允许输入整数部分最多(n-m)位,小数部分最多m位,如(n-m)9.m9,请重新输入" );            }        }



0 0
原创粉丝点击