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

    public class TextBoxNumber : TextBox
    {
        public TextBoxNumber()
        {
            this.KeyPress += textBox_KeyPress;
            this.Leave += textBox_Leave;
        }

        double _maxValue = int.MaxValue;

        double _minValue = int.MinValue;

        bool _isInt = true;

        [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(".") || _isInt)
                        {
                            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 IntValue
        {
            get
            {
                int ret = 0;
                int.TryParse(this.Text, out ret);
                return ret;
            }
            set
            {
                this.Text = value.ToString();
            }
        }

        public double DoubleValue
        {
            get
            {
                double ret = 0;
                double.TryParse(this.Text, out ret);
                return ret;
            }
            set
            {
                this.Text = value.ToString();
            }
        }


    }
发布了31 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/breakbridge/article/details/100666676