有一个文本框a.text,3个Label的Text值分别为我, 你, 他。现在要在文本框中输入一段公式,例如:我+你*他,或者:我/他 等,但是输入的必须是这3个Label的Text值,后台验证控件怎么验证输入的正确

解决方案 »

  1.   

    可以满足前面三个文本框输入字符串
    private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
            {
                bool result = true;
                byte[] t1 = Encoding.ASCII.GetBytes(textBox1.Text);
                byte[] t2 = Encoding.ASCII.GetBytes(textBox2.Text);
                byte[] t3 = Encoding.ASCII.GetBytes(textBox3.Text);            foreach (byte b in t1)
                {
                    if (e.KeyChar == Encoding.Default.GetChars(t1)[0])
                        result = false;
                }            foreach (byte b in t2)
                {
                    if (e.KeyChar == Encoding.Default.GetChars(t2)[0])
                        result = false;
                }            foreach (byte b in t3)
                {
                    if (e.KeyChar == Encoding.Default.GetChars(t3)[0])
                        result = false;
                }            e.Handled = result;
            }
      

  2.   

    抱歉,改一下 private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
            {
                bool result = true;
                string str = textBox1.Text + textBox2.Text + textBox3.Text;
                byte[] t = Encoding.ASCII.GetBytes(str);            foreach (byte b in t)
                {
                    if (e.KeyChar == b)
                        result = false;
                }            e.Handled = result;
            }
      

  3.   

    怎么找不到private void textBox4_KeyPress(object sender, KeyPressEventArgs e) 这个事件  我做的是网站 不好意思 新手一枚 大神多加指点一下 顺便加点注释吧 万分谢谢
      

  4.   

    网页很久没接触了,不是很了解。我用了TextChanged事件,但是要在回传了才进入,而不是你要的边输入边判断,代码在下面,你可要调用下面代码放到能马上触发的事件里,下面是代码
    这截取的方法比较愚蠢,你可以改用前台正则验证protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                string str = textBox1.Text + textBox2.Text + textBox3.Text;
                int keycharvalue = Encoding.ASCII.GetBytes(TextBox1.Text.Substring(TextBox1.Text.Length - 1,1))[0];
                if (EnterJudgmentMethod(keycharvalue, str))
                    TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1);
            }
            /// <summary>
            /// 控制键盘输入方法
            /// </summary>
            /// <param name="KeyCharValue">键盘输入的ASCII值——e.KeyChar值</param>
            /// <param name="str">输入的字符集合</param>
            /// <returns></returns>
            public static bool EnterJudgmentMethod(int KeyCharValue, string str)
            {
                bool result = true;
                byte[] t = Encoding.ASCII.GetBytes(str);            foreach (byte b in t)
                {
                    if (KeyCharValue == b)
                        result = false;
                }
                return result;
            }
      

  5.   


    你要是每次输入的字符串不是很长,你就以此判断一下呗
    你可以写个函数        public static bool HasTheWorld(string strTest)
            {
                bool hasWorldFlag = false;
                string strBase = "+—*/你我他";
                if (strBase.Contains(strTest))
                {
                    hasWorldFlag = true;
                }
                return hasWorldFlag;
            }其中strTest为你需要判断字符串中的每个字符,你可以循环去判断,但发现有一个字符不在你规定的字符中,判断结束