c#数字文本框控件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



namespace wfp.classlibrary

{

    public partial class BASC_NUMTEXT : TextBox

    {

        public BASC_NUMTEXT(int MaxLength, DataTable DT)

        {

      

            InitializeComponent();

            MaxLength = MaxLength < 0 ? 100 : MaxLength;

            base.Width = MaxLength < 100 ? 100 : (MaxLength > 255 ? 255 : MaxLength);

            base.MaxLength = MaxLength;

            meColor = this.BackColor;

        }

        private string vformat;

        private bool isformat = true;

        public string TextFormat

        {

            get { return vformat; }

            set

            {

                vformat = value;

            }

        }

        [Description("是否执行格式化")]

        public bool IsFormat

        {

            get { return isformat; }

            set

            {

                isformat = value;

            }

        }



        #region 限制输入数字

        private string strText;

        private int iCur;

        private Color meColor;



        protected override bool ProcessKeyEventArgs(ref   Message m)

        {

            int s = m.WParam.ToInt32();

            string xsfh = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;

            int xsfg = System.Text.Encoding.Default.GetBytes(xsfh)[0];

            #region



            if (s == 46)//判断是否小数点

            {

                if (xsfg != 46)

                {

                    m.WParam = new IntPtr(xsfg);

                }

                if (this.Text.Contains(xsfh) == true)

                {

                    return true;

                }

            }

            else if (s == xsfg)

            {

                if (this.Text.Contains(xsfh) == true)

                {

                    return true;

                }

            }

            else if (s != 17 && s != 1 && s != 65 && s != 99 && s != 118) //ctrl 控制 a c v

            {

                if (m.Msg == 0x0102 || m.Msg == 257) //|| m.Msg == 256

                {

                    if ((s != 45 && s < 48 & s != 8 && s != 13 && s != 9 && s != 37 && s != 39 && s != 36 && s != 35 && s != 46)

                       || (s > 57 && s != 189 && s != 110))

                        return true;

                }

            }

            else if (s == 65)

            {

                this.SelectAll();

            }

            else if (s == 99)

            {

                if (string.IsNullOrEmpty(this.SelectedText) == false)

                {

                    Clipboard.SetText(this.SelectedText);

                }

                return true;

            }

            else if (s == 118)

            {

                if (string.IsNullOrEmpty(Clipboard.GetText()) == false)

                {

                    this.SelectedText += Clipboard.GetText();

                }

                return true;

            }



            strText = this.Text;

            iCur = this.SelectionStart;

            #endregion

            return base.ProcessKeyEventArgs(ref   m);

        }

        protected override void OnLeave(EventArgs e)

        {

            RunFormat();

            base.OnLeave(e);

        }

        protected override void OnTextChanged(EventArgs e)

        {

         string xsdfg = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;

            string regstr;

            if (xsdfg != ".")

            {

                regstr = @"^(-?[0-9]*[" + xsdfg + "]*[.]*[0-9]{0,8})$";

            }

            else

            {

                regstr = @"^(-?[0-9]*[.]*[0-9]{0,8})$";

            }



            System.Text.RegularExpressions.Regex reg

              = new System.Text.RegularExpressions.Regex(regstr);

            if (reg.IsMatch(this.Text))

            {

                if (this.Text.Length == 30)

                {

                    this.Text = strText;

                    this.SelectionStart = iCur;

                    this.BackColor = System.Drawing.Color.FromName("mistyrose");

                }

                else if (this.BackColor == System.Drawing.Color.FromName("mistyrose"))

                {

                    this.BackColor = meColor;

                }

            }

            else

            {

                if (WFPPublic.DataCtrl.IsPositveDecimal(this.Text) == false)

                {

                    this.Text = strText;

                   

                }

                this.SelectionStart = iCur;

            }

            if (this.Focused == false)

            {

                RunFormat();

            }

            base.OnTextChanged(e);

        }

 

        private void RunFormat()

        {

            if (isformat == true)

            {

                if (string.IsNullOrEmpty(this.Text) == true)

                {

                    return;

                }

                try

                {

                    decimal dv = decimal.Parse(this.Text);

                    if (string.IsNullOrEmpty(vformat) == false)

                    {

                        // this.Text = string.Format(vformat, dv);// dv.ToString(vformat);

                        this.Text = dv.ToString(vformat);

                    }

                    else

                    {

                        string constr = "0";

                        if (constr == "")

                        {

                            this.Text = dv.ToString("###########################.######");

                        }

                        else if (constr == "0")

                        {

                            this.Text = dv.ToString("##########################0.######");

                        }

                        else

                        {

                            int ovs = WFPPublic.DataCtrl.StrToInt(constr);

                            if (ovs > 0)

                            {

                                string fz = "0";

                                this.Text = dv.ToString("##########################0." + fz.PadLeft(ovs, '0') + "#####");

                            }

                        }

                    }

                }

                catch

                { }

            }

        }

        #endregion

    }

}



你可能感兴趣的:(文本框)