欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#自定义控件之数字文本框

程序员文章站 2022-03-04 11:55:02
...
    public class TextBoxNumber : TextBox
    {
        public TextBoxNumber()
        {
            this.KeyPress += textBox_KeyPress;
            this.Leave += textBox_Leave;
        }

        double _maxValue = int.MaxValue;

        double _minValue = int.MinValue;

        bool _isInt = false;

        [Category("自定义"), Description("显示的小数位数")]
        public int DecimalPlace { get; set; }

        [Category("自定义"), Description("是否是Int类型")]
        public bool IsInt
        {
            get
            {
                return _isInt;
            }

            set
            {
                _isInt = value;
                checkText();
            }
        }

        [Category("自定义"), Description("显示的最大值")]
        public double MaxValue
        {
            get
            {
                return _maxValue;
            }

            set
            {
                _maxValue = value;
            }
        }

        [Category("自定义"), Description("显示的最小值")]
        public double MinValue
        {
            get
            {
                return _minValue;
            }

            set
            {
                _minValue = value;
            }
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }

            set
            {
                base.Text = value;
                checkText();
            }
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int acsii = e.KeyChar;
            bool isNumber = acsii >= 48 && acsii <= 57;
            if (!isNumber)
            {
                int selectionStart = tb.SelectionStart;
                switch (acsii)
                {
                    case 8:  //删除
                    case 13: //回车
                        break;
                    case 43:  //+
                    case 45:  //-
                        if (selectionStart != 0)
                        {
                            e.Handled = true;
                        }
                        break;
                    case 46:  //.
                        if (tb.Text.Contains("."))
                        {
                            e.Handled = true;
                        }
                        else if (tb.Text.StartsWith("-") || tb.Text.StartsWith("+"))
                        {
                            if (selectionStart <= 1)
                            {
                                e.Handled = true;
                            }
                        }
                        else if (selectionStart == 0)
                        {
                            e.Handled = true;
                        }
                        break;
                    default:
                        e.Handled = true;
                        break;
                }
            }
        }

        private void textBox_Leave(object sender, EventArgs e)
        {
            checkText();
        }

        void checkText()
        {
            double ret = 0;
            double.TryParse(this.Text, out ret);
            if (ret >= MaxValue)
            {
                ret = MaxValue;
            }
            else if (ret <= MinValue)
            {
                ret = MinValue;
            }
            if (_isInt)
            {
                ret = (int)ret;
            }
            else
            {
                ret = Math.Round(ret, DecimalPlace);
            }
            string newText = ret.ToString();
            if (this.Text != newText)
            {
                this.Text = newText;
            }
        }

        public int ToInt32()
        {
            int ret = 0;
            int.TryParse(this.Text, out ret);
            return ret;
        }

        public double ToDouble()
        {
            double ret = 0;
            double.TryParse(this.Text, out ret);
            return ret;
        }


    }

 

相关标签: 控件