加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

对于KEYPRESS里的正则输入,格式要求为*.xx 保留2位小数

发布时间:2020-12-14 04:36:50 所属栏目:百科 来源:网络整理
导读:private void Cells_KeyPress(object sender,KeyPressEventArgs e) //自定义事件 { if (this.dataGridView1.CurrentCellAddress.X == 2) { string aa = @"^d+$"; string bb = @"^.{0,1}$"; Regex reg1 = new Regex(aa); Regex reg2 = new Regex(bb); if (!

private void Cells_KeyPress(object sender,KeyPressEventArgs e) //自定义事件 { if (this.dataGridView1.CurrentCellAddress.X == 2) { string aa = @"^d+$"; string bb = @"^.{0,1}$"; Regex reg1 = new Regex(aa); Regex reg2 = new Regex(bb); if (!reg1.IsMatch(e.KeyChar.ToString()) && !reg2.IsMatch(e.KeyChar.ToString())) e.Handled = true; string cc = @"^d+(.{0,1}d{0,1})$";//@"^(-?d+)(.d+)?$"; char[] point={'.'}; string[] seperated=CellEdit.Text.Split(point); if(seperated.Length>1) //取小数点后字符串 { if (seperated[1].Length >= 2 //.22,小数点后的数字长度超过2 //&& (e.KeyChar >= '0' && e.KeyChar <= '9')//同时又在点后输入数字 && CellEdit.SelectionLength <= 0 //没有选中当前所输入的数字 && CellEdit.SelectionStart>=CellEdit.Text.Length-1//光标不在最后 ) cc = @"^d+(.{0,1})$"; else cc = @"^d+(.{0,2})$"; } else //万一number没有小数点,置零 cc = @"^d+(.{0,1})$"; Regex reg3 = new Regex(cc); // Regex reg = new Regex("^d*.{0,1}d{0,2}$");//Regex("^[0-9]{1}([0-9]|[.])$");"^" if ((!reg3.IsMatch(CellEdit.Text) && CellEdit.Text != "0")|| (e.KeyChar == '.' && CellEdit.Text.Contains('.'))) e.Handled = true;// if (!(e.KeyChar >= '0' && e.KeyChar <= '9') if (e.KeyChar == 'b') e.Handled = false; } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读