*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国  http://www.bc-cn.net
*/ 作者: C_B_Lu        QQ:184118549
*/ 时间: 2007-9-4  编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
在维护界面中,常常需要对在TextBox中输入的数据进行验证,以保证其有效性,
以前自己也有在论坛中发表过,这次做了些小的改进,不过感觉还是有些繁锁,然与大家一起讨论类似问题.
如下例,是一个保确只输入带两位小数的数字,如果配合设置其TextBox控件的MaxLength属性,可以控制该数值的大小.
注: tbPartsCost为一个TextBox控件.tbPartsCost_KeyPress和tbPartsCost_Validating分别是该控件的KeyPress事件和Validating事件.
        private void tbPartsCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender; 
            int curPos = tb.SelectionStart;    // 當前光標位置
            int pointPos = tb.Text.LastIndexOf('.');    // 小數點的位置
            if ((pointPos > 0) && (curPos > (pointPos + 2)))  // 輸入超過兩位小數﹐
            {
                e.Handled = true;
                return;
            }
            string strInt = tb.Text.Trim().Substring(0, curPos);
            string strDec= "";
            if(pointPos <0)
            {
                strDec = ".00";
            }
            else
            {
                strDec = tb.Text.Trim().Substring(pointPos);
                if (strDec.Length > 3)
                {
                    strDec = strDec.Substring(0, 3);
                }
                else
                {
                    strDec = strDec.PadRight(3, '0');
                }
            }     
            if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '.') && (e.KeyChar != '\b'))
            {  //  如果用戶輸入的不是字符~9﹐也不是刪除鍵和小數點﹐則不顯示﹐
                e.Handled = true;    // 不允許輸入
            }
            else if (e.KeyChar == '.')
            {
                e.Handled = true;         
                if (strInt.Length == 0)
                {
                    strInt = "0";
                }
                tb.Text = strInt + strDec;
                tb.SelectionStart = ++curPos;
            }
            else
            {
                e.Handled = false;                
                if (curPos > pointPos)
                {
                    tb.SelectionLength = 1;
                }
                else if (curPos == pointPos)
                {
                    tb.SelectionStart = ++curPos;
                    SendKeys.Send(e.KeyChar.ToString());
                }
                if (pointPos < 0)
                {
                    tb.Text = strInt + strDec;
                    pointPos = tb.Text.LastIndexOf('.');    // 小數點的位置
                }
                if ((curPos < pointPos) && (tb.Text.Trim().Length == tb.MaxLength))
                {
                    tb.SelectionLength = 1;
                }
                tb.SelectionStart = curPos;
            }
        }
        
        private void tbPartsCost_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            string[] strs = tb.Text.Split(new char[] { '.' });
            if (strs[0].Trim() == "")
            {
                strs[0] = "0";
            }
            strs[0] =int.Parse( strs[0]).ToString().Trim();
            if (strs[0].Length > tb.MaxLength - 3)
            {
                strs[0] = strs[0].Substring(0, tb.MaxLength - 3);
            }
            strs[1] = strs[1].Trim();
            if (strs[1].Length < 2)
            {
                strs[1] = strs[1].PadRight(2, '0');
            }
            tb.Text = strs[0] + "." + strs[1];
        }

 
											





 
	    

 
										
					
	
 
											