Input and only allows custom type int Decimal String

Custom only put a TextBox control, and use the custom control at TextBox Paint draw a line, then to the TextBox custom made three attributes, namely TextBoxText properties, convenient value; IntBool properties, whether enter only positive integer; if inputDecimal can only enter decimals.

The following is the entire custom source code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace GCIMS.CommonCtrl
{
    public partial class ctrlUnderlineTextBox : UserControl
    {
        #region 属性
        /// <summary>
        /// 控件的文本
        /// </summary>
        [Description("文本值"), Browsable(true), Category("自定义属性")]
        public string TextBoxText
        {
            get
            {
                return textBox.Text;
            }
            set
            {
                textBox.Text = value;
            }
        }

        private bool intBool;
        /// <summary>
        /// 只输入正整数
        /// </summary>
        [Description("只输入正整数"), Browsable(true), Category("自定义属性"), DefaultValue(false)]
        public bool IntBool
        {
            get
            {
                return intBool;
            }
            set
            {
                intBool = value;
                if (intBool)
                {
                    textBox.KeyPress += new KeyPressEventHandler(IntBool_KeyPress);
                }
            }
        }

        private bool inputDecimal;
        /// <summary>
        /// 只输入小数
        /// </summary>
        [Description("只输入小数"), Browsable(true), Category("自定义属性"), DefaultValue(false)]
        public bool InputDecimal
        {
            get
            {
                return inputDecimal;
            }
            set
            {
                inputDecimal = value;
                if (inputDecimal)
                {
                    textBox.KeyPress += new KeyPressEventHandler(InputDecimal_KeyPress);
                }
            }
        }
        #endregion

        #region 构造函数
        public ctrlUnderlineTextBox()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件-ctrlUnderlineTextBox_KeyPress
        private void IntBool_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 && textBox.Text.Length < 18 || (int)e.KeyChar == 8) //只能输入0-9数字和BackSpace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        #endregion

        #region 事件-InputDecimal_KeyPress
        private void InputDecimal_KeyPress(object sender, KeyPressEventArgs e)
        {
            //搜索字符串中的'.'字符
            string textBoxStr = textBox.Text;
            Regex rg = new Regex(".");
            MatchCollection mc = rg.Matches(textBoxStr);
            int textBoxCount = mc.Count;

            //允许输入数字,小数点,退格键,不允许输入大于18为的数字,不允许输入两个小数点
            if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 && textBox.Text.Length < 18 || (int)e.KeyChar == 8 || e.KeyChar == '.' && textBoxCount <= 1) //只能输入0-9数字和BackSpace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        #endregion

        #region 事件-CtrlUnderlineTextBox_Paint
        private void CtrlUnderlineTextBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.Handle);
            System.Drawing.Pen pen = new Pen(Color.Black);
            PointF point1 = new PointF(textBox.Location.X, textBox.Location.Y + textBox.Height);
            PointF point2 = new PointF(textBox.Location.X + textBox.Width, textBox.Location.Y + textBox.Height);
            g.DrawLine(pen, point1, point2);
        }
        #endregion
    }
}




Reproduced in: https: //my.oschina.net/dongri/blog/610931

Guess you like

Origin blog.csdn.net/weixin_34358092/article/details/91765938