zoukankan      html  css  js  c++  java
  • 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();
                }
            }
    
    
        }
    
  • 相关阅读:
    ios input readonly失效(点击的时候会有光标出现)/禁止输入法弹出问题
    sublime格式化
    菜单栏展开关闭效果(1)
    做数字判断显示相应的图标
    判断img的src为空/点击时候两张图片来回替换
    numpy
    pat甲级1085
    pat甲级1107
    2018.9.8pat秋季甲级考试
    pat甲级1044二分查找
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709078.html
Copyright © 2011-2022 走看看