• DataGridView控制单元格修改的输入规则


            // 修改值
            private void dgvOrderIn_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                DataGridViewTextBoxEditingControl CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                CellEdit.SelectAll();
                CellEdit.KeyPress -= new KeyPressEventHandler(Cells_Number_KeyPress);
                CellEdit.KeyPress -= new KeyPressEventHandler(Cells_Price_KeyPress);
                switch (this.dgvOrderIn.CurrentCellAddress.X)
                {
                    case 4: //数量
                        CellEdit.KeyPress += new KeyPressEventHandler(Cells_Number_KeyPress);
                        break;
                    case 3: //单价
                    case 5: //折扣率
                        CellEdit.KeyPress += new KeyPressEventHandler(Cells_Price_KeyPress);
                        break;
                    default:
                        break;
                }
            }
    
            // 键盘输入数量控制
            private void Cells_Number_KeyPress(object sender, KeyPressEventArgs e)
            {
                int kc = Convert.ToInt32(e.KeyChar);
                // 8    backspace退格
                // 13   回车
                // 46   .
                // 48   0
                // 57   9 
                if ((kc < 48 || kc > 57) && kc != 8 && kc != 13)
                {
                    e.Handled = true;
                }
                TextBox tb = (TextBox)sender;
                string strMathchValue = tb.Text;
                //判断是否为退格键
                if (kc == 8)
                {
                    ///判断输入控件选择文本的长度 
                    if (tb.SelectionLength == 0)
                    {//未选文本中时处理
                        if (tb.SelectionStart > 0)
                        {//选择的开始位置大于0时处理
                            //strMathchValue.Remove(tb.SelectionStart, 1);
                            strMathchValue.Remove(tb.SelectionStart - 1, 1);
                        }
                    }
                    else
                    {//有选中文本时处理
                        strMathchValue.Remove(tb.SelectionStart, tb.SelectionLength);
                    }
                }
                else
                {
                    //将当前输入的字符号插入到原来的字符串中
                    strMathchValue = strMathchValue.Insert(((TextBox)sender).SelectionStart, e.KeyChar.ToString());
                }
    
                Regex rx = new Regex(@"^\+?[1-9][0-9]*$", RegexOptions.IgnoreCase);
                if (rx.IsMatch(strMathchValue) == false)
                {
                    e.Handled = true;
                }
            }
    
            // 键盘输入价格控制
            private void Cells_Price_KeyPress(object sender, KeyPressEventArgs e)
            {
                int kc = Convert.ToInt32(e.KeyChar);
                if ((kc < 48 || kc > 57) && kc != 8 && kc != 13 && kc != 46)
                {
                    e.Handled = true;
                }
                TextBox tb = (TextBox)sender;
                string strMathchValue = tb.Text;
                if (kc == 8)
                {
                    if (tb.SelectionLength == 0)
                    {
                        if (tb.SelectionStart > 0)
                        {
                            strMathchValue.Remove(tb.SelectionStart - 1, 1);
                        }
                    }
                    else
                    {
                        strMathchValue.Remove(tb.SelectionStart, tb.SelectionLength);
                    }
                }
                else
                {
                    strMathchValue = strMathchValue.Insert(((TextBox)sender).SelectionStart, e.KeyChar.ToString());
                }
                Regex rx = new Regex(@"^[0-9]+\.{0,1}[0-9]{0,2}$", RegexOptions.IgnoreCase);
                if (rx.IsMatch(strMathchValue) == false)
                {
                    e.Handled = true;
                }
            }
  • 相关阅读:
    Facebook第三方登录切换账号的问题
    内部类中调用外部变量
    前端还原UI设计图CSS样式工具(ps+蓝湖+PxCook+imgcook)
    git 报错 eslint fix found some errors. Please fix them and try committing again.
    在vue中使用vuebaidumap实现地址定位标点及关键字搜索定位(获取地址数据并获取经纬度)
    elementui的时间范围组件,限制选了第一个时间后,第二个只能在第一个的前后一周
    JavaScript API v2.0+ v3.0
    BFC块级格式化上下文
    解决小程序api无法返回promise对象
    移动端适配 视口(viewport)和vw、vh、rem
  • 原文地址:https://www.cnblogs.com/linji/p/2738249.html
Copyright © 2020-2023  润新知