当前位置: 首页 > article >正文

vs中在工具箱添加自定义控件numberTextBox

在winform中没有numberTextBox控件,为了将numberTextBox添加到工具箱当中,于是参考以下博客封装了一个自定义numberTextBox控件:

winform 自定义数值(数字)输入框_c#如何设置一个框输入值-CSDN博客

接下来介绍以下封装和引用的过程:

1.先创建一个C#的生成dll的工程,然后将以下代码进去

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Widgets
{
    /// <summary>
    /// 数值输入框
    /// </summary>
    [ToolboxItem(true)]
    public class NumberTextBox : TextBox
    {
        /// <summary>
        /// 输入数值类型
        /// </summary>
        private NumberType numberType;

        /// <summary>
        /// 输入小数点,默认2位
        /// </summary>
        private int decimalPointNumber;

        /// <summary>
        /// 允许输入的最小值,空值即代表当前类型的最小值
        /// </summary>
        private string minValue;

        /// <summary>
        /// 允许输入的最大值,空值即代表当前类型的最大值
        /// </summary>
        private string maxValue;

        [Browsable(true)]
        [Category("NumberTextBox"), Description("指定控件数值输入的最小值"), DisplayName("最小值")]
        public string MinValue
        {
            get
            {
                return minValue;

            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    minValue = "";
                }
                else if (numberType == NumberType.Integer)
                {
                    if (int.TryParse(value, out int result))
                    {
                        minValue = result.ToString();
                    }
                    else
                    {
                        minValue = "";
                    }

                }
                else if (numberType == NumberType.Long)
                {
                    if (long.TryParse(value, out long result))
                    {
                        minValue = result.ToString();
                    }
                    else
                    {
                        minValue = "";
                    }

                }
                else if (numberType == NumberType.Float)
                {
                    if (float.TryParse(value, out float result))
                    {
                        minValue = result.ToString("F" + decimalPointNumber);
                    }
                    else
                    {
                        minValue = "";
                    }

                }
                else if (numberType == NumberType.Double)
                {
                    if (double.TryParse(value, out double result))
                    {
                        minValue = result.ToString("F" + decimalPointNumber);
                    }
                    else
                    {
                        minValue = "";
                    }
                }
            }
        }

        [Browsable(true)]
        [Category("NumberTextBox"), Description("指定控件数值输入的最大值"), DisplayName("最大值")]
        public string MaxValue
        {
            get
            {
                return maxValue;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    maxValue = "";
                }
                else if (numberType == NumberType.Integer)
                {
                    if (int.TryParse(value, out int result))
                    {
                        maxValue = result.ToString();
                    }
                    else
                    {
                        maxValue = "";
                    }

                }
                else if (numberType == NumberType.Long)
                {
                    if (long.TryParse(value, out long result))
                    {
                        maxValue = result.ToString();
                    }
                    else
                    {
                        maxValue = "";
                    }

                }
                else if (numberType == NumberType.Float)
                {
                    if (float.TryParse(value, out float result))
                    {
                        maxValue = result.ToString("F" + decimalPointNumber);
                    }
                    else
                    {
                        maxValue = "";
                    }

                }
                else if (numberType == NumberType.Double)
                {
                    if (double.TryParse(value, out double result))
                    {
                        maxValue = result.ToString("F" + decimalPointNumber);
                    }
                    else
                    {
                        maxValue = "";
                    }
                }
            }
        }

        [Browsable(true)]
        [Category("NumberTextBox"), Description("指定控件数值输入类型"), DisplayName("数值类型")]
        public NumberType NumberType
        {
            get => numberType; set => numberType = value; }

            [Browsable(true)]
            [Category("NumberTextBox"), Description("当指定输入数值类型为小数时保留小数点位数"), DisplayName("小数点位数")]
            [DefaultValue(2)]
            public int DecimalPointNumber
        {
            get => decimalPointNumber; set => decimalPointNumber = value; }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (string.IsNullOrWhiteSpace(Text) || Text == "-")//输入负值
            {
                return;
            }
            switch (numberType)
            {
                case NumberType.Integer:
                    if (!int.TryParse(Text, out var i))
                    {
                        Text = Text.Substring(0, Text.Length - 1);
                        SelectionStart = Text.Length;
                    }
                    break;
                case NumberType.Long:
                    if (!long.TryParse(Text, out var l))
                    {
                        Text = Text.Substring(0, Text.Length - 1);
                        SelectionStart = Text.Length;
                    }
                    break;
                case NumberType.Float:
                    if (!float.TryParse(Text, out var f))
                    {
                        Text = Text.Substring(0, Text.Length - 1);
                        SelectionStart = Text.Length;
                    }
                    break;
                case NumberType.Double:
                    if (!double.TryParse(Text, out var d))
                    {
                        Text = Text.Substring(0, Text.Length - 1);
                        SelectionStart = Text.Length;
                    }
                    break;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Enter)
            {
                VerifyThenFormatValue();
            }
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            VerifyThenFormatValue();

        }

        private void VerifyThenFormatValue()
        {
            if (string.IsNullOrWhiteSpace(Text))
            {
                return;
            }
            if (numberType == NumberType.Float)
            {
                Text = float.Parse(Text).ToString("F" + decimalPointNumber).ToString();
            }
            else if (numberType == NumberType.Double)
            {
                Text = double.Parse(Text).ToString("F" + decimalPointNumber).ToString();
            }
            if (!string.IsNullOrWhiteSpace(minValue))
            {
                if (numberType == NumberType.Integer)
                {
                    if (int.Parse(Text) < int.Parse(minValue))
                    {
                        Text = minValue;
                    }
                }
                else if (numberType == NumberType.Long)
                {
                    if (long.Parse(Text) < long.Parse(minValue))
                    {
                        Text = MinValue;
                    }
                }
                else if (numberType == NumberType.Float)
                {
                    if (float.Parse(Text) < float.Parse(minValue))
                    {
                        Text = minValue;
                    }
                }
                else if (numberType == NumberType.Double)
                {
                    if (double.Parse(Text) < double.Parse(minValue))
                    {
                        Text = minValue;
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(maxValue))
            {
                if (numberType == NumberType.Integer)
                {
                    if (int.Parse(Text) > int.Parse(maxValue))
                    {
                        Text = maxValue;
                    }
                }
                else if (numberType == NumberType.Long)
                {
                    if (long.Parse(Text) > long.Parse(maxValue))
                    {
                        Text = maxValue;
                    }
                }
                else if (numberType == NumberType.Float)
                {
                    if (float.Parse(Text) > float.Parse(maxValue))
                    {
                        Text = maxValue;
                    }
                }
                else if (numberType == NumberType.Double)
                {
                    if (double.Parse(Text) > double.Parse(maxValue))
                    {
                        Text = maxValue;
                    }
                }
            }
            SelectionStart = Text.Length;
        }

        /// <summary>
        /// 复写Text,确保值是符合我们预设的类型
        /// </summary>
        public override string Text
        {
            get => base.Text;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    base.Text = value;
                }
                else
                {
                    if (numberType == NumberType.Integer)
                    {
                        if (int.TryParse(value, out var result))
                        {
                            base.Text = result.ToString();
                        }
                        else
                        {
                            base.Text = "";
                        }
                    }
                    if (numberType == NumberType.Long)
                    {
                        if (long.TryParse(value, out var result))
                        {
                            base.Text = result.ToString();
                        }
                        else
                        {
                            base.Text = "";
                        }
                    }
                    else if (numberType == NumberType.Float)
                    {
                        if (float.TryParse(value, out var result))
                        {
                            base.Text = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            base.Text = "";
                        }
                    }
                    else if (numberType == NumberType.Double)
                    {
                        if (double.TryParse(value, out var result))
                        {
                            base.Text = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            base.Text = "";
                        }
                    }
                }
            }
            }

            [Browsable(false)]
            [DefaultValue(0)]
            public int IntValue
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Integer)
                {
                    return 0;
                }
                VerifyThenFormatValue();
                return int.Parse(Text);
            }
            set
            {
                Text = value.ToString();
                VerifyThenFormatValue();
            }
        }

        [Browsable(false)]
        [DefaultValue(0)]
        public long LongValue
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Long)
                {
                    return 0;
                }
                VerifyThenFormatValue();
                return long.Parse(Text);
            }
            set
            {
                Text = value.ToString();
                VerifyThenFormatValue();
            }
        }

        [Browsable(false)]
        [DefaultValue(0)]
        public float FloatValue
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Float)
                {
                    return 0;
                }
                VerifyThenFormatValue();
                return float.Parse(Text);
            }
            set
            {
                Text = value.ToString();
                VerifyThenFormatValue();
            }
        }
        [Browsable(false)]
        [DefaultValue(0)]
        public double DoubleValue
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Double)
                {
                    return 0;
                }
                VerifyThenFormatValue();
                return double.Parse(Text);
            }
            set
            {
                Text = value.ToString();
                VerifyThenFormatValue();
            }
        }

    }

    public enum NumberType
    {
        Integer, Long, Float, Double
    }
}

2.然后在vs的工具箱中右键所有Windows窗体--》选择项--》选择浏览,然后把根据步骤1生成的dll添加进来--》接下来在工具箱就可以看到添加的numberTextBox控件了

3.编译好的dll也可以从这下载,省的自己编译了https://download.csdn.net/download/qq_42987967/89703930


http://www.kler.cn/a/287672.html

相关文章:

  • 【3D Slicer】的小白入门使用指南九
  • NVR录像机汇聚管理EasyNVR多品牌NVR管理工具视频汇聚技术在智慧安防监控中的应用与优势
  • Controller Baseband commands速览
  • 【Android原生问题分析】夸克、抖音划动无响应问题【Android14】
  • Scratch 014生日贺卡(上)
  • RabbitMQ教程:路由(Routing)(四)
  • 链表题总结
  • 使用MySql
  • [YM]课设-C#-WebApi-Vue-员工管理系统 (六)前后端交互
  • 二手电脑配置给你不一样的成就感之四
  • SQLite3 数据类型深入全面讲解
  • B3918 [语言月赛 202401] 图像变换
  • 力扣375.猜数字大小 II
  • AI时代,需要什么样的服务器操作系统?
  • 51单片机-定时器时钟
  • docker实战基础五(Dockerfile)
  • 2024年【电气试验】新版试题及电气试验证考试
  • 请解释Java中的装箱拆箱操作对性能的影响,并讨论其最佳实践。什么是Java中的值传递和引用传递?它们在函数调用中的表现有何不同?
  • redis主从+高可用切换+负载均衡
  • react 列表页面中管理接口请求的参数
  • 当前开发技术的未来发展:趋势、机遇与挑战
  • Spring Cloud Stream与Kafka(一)
  • 【网络安全】Bingbot索引投毒实现储存型XSS
  • 华为OD机试真题 - 拼接URL(Python/JS/C/C++ 2024 D卷 100分)
  • RabbitMQ当消息消费失败时,会重新进入队列吗?
  • skywalking接入nginx