关于textbox或combobox限制输入内容
2015-10-20 10:10
232 查看
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,请重新输入" ); } }
相关文章推荐
- svn分支开发与主干合并(branch & merge)
- java里程碑之泛型--泛型基本语法
- mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI
- LeetCode OJ:House Robber(住宅窃贼)
- 字典转JSON和 数组转JSON
- 来自西弗吉利亚大学li xin整理的CV代码合集
- 神奇的HTML5离线存储(应用程序缓存)
- 64位arm_Linux操作系统驱动兼容性问题
- STL中关于map和set的一些问题
- SQL8数据库定期自动备份
- 基于ArcGIS API for JavaScript的统计图表实现
- Windows Update 时出现8024402C的错误
- 函数式编程收藏
- DotNet中的HTTP操作
- 遍历Map的四种方法
- 【Fix】自己动动手修理鼠标滚轮回跳
- 扩展GridView控件――为内容项添加拖放及分组功能
- 给xcode添加预编文件的方法
- Qt学习笔记 线程(一)
- unity安卓打包的问题