private void textBox_cardID_TextChanged(object sender, EventArgs e)
        {
            string str = textBox_cardID.Text;
            if (!CharCaps.CheakLength(str, 8))
            {
                textBox_cardID.Text = ADDRESSCARDID;
                textBox_cardID.SelectionStart = ADDRESSCARDID.Length;
                return;
            }
            ADDRESSCARDID = str;
        }

        private void textBox_cardID_KeyPress(object sender, KeyPressEventArgs e)
        {
            //'\b'为删除键
            if (!((e.KeyChar <= 'z' && e.KeyChar >= 'a') || (e.KeyChar <= 'Z' && e.KeyChar >= 'A') || e.KeyChar == '\b' || e.KeyChar == '\r'))//如果输入的数字
            {
                e.Handled = true;//处理KeyPress事件
            }
        }

  

 //获取字符串的长度。
        public static int GetLength(string str)
        {
            if (str.Length == 0) return 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0; byte[] s = ascii.GetBytes(str);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }
            }
            return tempLen;
        }

        public static bool CheakLength( string value,int len)
        {
            if (GetLength(value) > len)
            {
                return false;
            }
            return true;
        }

  

posted on 2013-12-10 12:03  EM_C  阅读(1076)  评论(0)    收藏  举报