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();
                }
            }
    
    
        }
    
  • 相关阅读:
    再论 ASP.NET 中获取客户端IP地址
    修改MariaDB 路径
    CentOS MariaDB 安装和配置
    asp.net core 使用protobuf
    Xamarin绑定微信SDK 实现分享功能
    iOS中转义后的html标签如何还原
    MvvmCross框架在XamarinForms中的使用入门
    Xamarin.Form 初学 之 服务引用-WCF服务引用
    程序员求职面试三部曲之三:快速适应新的工作环境
    程序员求职面试三部曲之二:提高面试的成功率
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709078.html
Copyright © 2011-2022 走看看