c#限制文本框输入数值

详细代码如下

/// <summary>
        /// 只能输入正整数 ,但可以粘贴进其他数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Uint32_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {

            }
            else
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 限制输入有符号整数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void int32_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {

            }
            else
            {
                //可以在首个位置输入 -
                if (e.KeyChar == '-' && ((System.Windows.Forms.TextBox)sender).SelectionStart == 0)
                {
                    // return;
                }
                else { 
                    e.Handled = true;
                }
            }
        }
        /// <summary>
        /// 只允许输入有符号小数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fp32_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {

            }
            else
            {
                //可以在首个位置输入 -
                if (e.KeyChar == '-' && ((System.Windows.Forms.TextBox)sender).SelectionStart == 0)
                {
                    // return;
                }
                else
                {
                    //小数点处理 只能输入一个小数点,并且小数点不能在首位
                    if(e.KeyChar == '.' &&
                    ((System.Windows.Forms.TextBox)sender).Text.IndexOf(".") == -1 &&
                    ((System.Windows.Forms.TextBox)sender).SelectionStart != 0)
                    {
                        //处理第一位是符号然后紧接着输入小数点
                        if (e.KeyChar == '.' &&
                            ((System.Windows.Forms.TextBox)sender).Text.IndexOf("-") == 0 &&
                            ((System.Windows.Forms.TextBox)sender).SelectionStart != 1)
                        {

                        }
                        else
                        {
                            e.Handled = true;
                        }
                    }
                    else {
                        e.Handled = true;
                        
                    }
                }
            }
        }

调用是使用

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            Uint32_textBox_KeyPress(sender, e);
        }


        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            int32_textBox_KeyPress(sender, e);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            fp32_textBox_KeyPress(sender, e);
        }

运行结果

 

网上写的好多效果不理想,特此重写。

此函数两个特点因为引用了sender对象,所以可以多个文本框一起调用,或者直接在textbox的keypress事件内直接引用均可以。

特此记录

anlog

2022年9月14日

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值