自定义控件只允许输入Decimal和int类型字符串

自定义控件中只放了一个TextBox控件,并在TextBox下利用自定义控件的Paint画了一条线,然后给TextBox做了3个自定义属性,分别是TextBoxText属性,方便取值;IntBool属性,是否只输入正整数;inputDecimal是否只能输入小数。

以下是整个自定义控件的源代码


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
    }
}




转载于:https://my.oschina.net/dongri/blog/610931

猜你喜欢

转载自blog.csdn.net/weixin_34358092/article/details/91765938