我是靠谱客的博主 明亮篮球,最近开发中收集的这篇文章主要介绍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日

最后

以上就是明亮篮球为你收集整理的c#限制文本框输入数值的全部内容,希望文章能够帮你解决c#限制文本框输入数值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部