要求:保存的是numeric类型,现在要求整数部分最多7位,小数部分最多两位,小数点只能一个,负号只能一个;只能输入数字或小数点或符号;当整数或小数输入的位数够了就不能再输入任何数字;不接受复制;总之把该限制的完全限制住,写成公共的方法,最好可以处理类似的所有控件,该如何写?
(之前一同事写过一个,不好用,问我来着,也没写好,汗呢,只好与其在此发帖)

解决方案 »

  1.   

    1.在textchange里判断值
    2.        protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                switch (m.Msg)
                { 
                    case 0X7B:
                    case 0X301:
                    case 770:
                        break;
                    default :
                        base.WndProc(ref m);
                        break;
                }
            }
      

  2.   

    ok,简单测试通过了public class NumericBox : TextBox
        {
            protected override void WndProc(ref Message m)//屏蔽指定消息
            {
                switch (m.Msg)
                {
                    case 0x0300://Cut
                    case 0x0301://Copy
                    case 0x0302://Paste
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }
            }        /// <summary>
            /// 常量,指定可以输入的整数位数
            /// </summary>
            protected const int intLen = 7;
            /// <summary>
            /// 常量,指定可以输入的小数位数
            /// </summary>
            protected const int decLen = 2;
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                int k = (int)e.KeyChar;
                bool isNeg = base.Text.StartsWith("-");
                int len = base.Text.Length;
                int pos = base.SelectionStart;
                int dotIndex = base.Text.IndexOf(".");
                if ((k >= 48 && k <= 57) || k == 8 || k == 46 || k == 45) //数字0-9 ,Backspace,.,-
                {
                    if (k == 8)//backspace接允许
                        e.Handled = false;
                    else if (k == 45 && (isNeg || pos != 0))//只能有一个"-",且只能在索引为零的位置上
                        e.Handled = true;
                    else if (k == 46)//只能有一个小数点
                    {
                        e.Handled = dotIndex != -1;
                    }
                    else
                    {
                        int elen = isNeg ? intLen + 1 : intLen;
                        if (dotIndex == -1)//还没有输入.时
                        {
                            if (elen == len && k != 46)//长度达到时,只能输入小数点了
                            {
                                e.Handled = true;
                            }
                        }
                        else
                        {
                            if (len == dotIndex + decLen + 1)//小数位数
                                e.Handled = true;
                        }
                    }            }
                else
                {
                    e.Handled = true;
                }
                base.OnKeyPress(e);
            }
        }
      

  3.   

    使用时,在设计文件里将new System.Windows.Forms.TextBox();改为NumericBox()即可
      

  4.   

    貌似vs有这样一个控件
    NumericUpDown
    这个控件就是输入小数的,有必要自己写一个吗?
      

  5.   

    zw880322那位兄弟,微软的那个NumericUpDown功能不够用,比如可以粘贴汉字,比如小数点都可以输入许多,比如位数限制等等很多问题,不觉得吗?呵呵,仍然感谢您的参与
      

  6.   

    新建一个类NumericBox ,然后把上面的代码复制过去,保存,工具箱里会自动添加一个NumericBox,拖到窗体上就可以了