窗体form1, 控件 一个textbox,一个button, 当用户在textbox中输入内容, 点击button后,如果textbox中的内容的第一个字符不是A或者B或者C 就弹出messegebox.show ("出错") 请问代码如何写啊;谢谢

解决方案 »

  1.   

    string fstr = textbox1.text.trim().substring(0,1);
    if(fstr!=A||fstr!=B||fstr!=C)
    {
     MessageBox.Show("出错") ;
    }
      

  2.   

    string a = textBox1.Text.Substring(0, 1).ToUpper();
                if (a == "A" || a == "B" || a == "C")
                { }
                else
                {
                    MessageBox.Show("出错");
                }
      

  3.   


     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1"
                runat="server" Text="Button" OnClientClick="return Check();" /> function Check() {
                var va = document.getElementById("TextBox1").value;
                
                if (va) {
                    var f = va.substring(0, 1);
                    alert(f);
                    if (f != "A" || f != "B" || f != "C") {
                        alert("出错");
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                else {
                    return false;
                }
            }
      

  4.   

    当入为空时,肯定会报substring异常
      

  5.   


    C# code
    char firstch = textbox1.text.trim()[0]
    if(fstr!='A'||fstr!='B'||fstr!='C')
    {
    MessageBox.Show("出错") ;
    }
      

  6.   

            private void buttonX2_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim().Length >= 1)
                {
                    char firstch = textBox1.Text.Trim()[0];
                    if (firstch != 'A' || firstch != 'B' || firstch != 'C')
                    {
                        MessageBox.Show("出错");
                    }
                }        }
      

  7.   

    用substring取第一个字符,判断就是了
      

  8.   

    if(txt1.Text!="")
    {
       string a = txt1.Text.Substring(0, 1).ToUpper();
       if (a != "A" || a != "B" || a != "C")
       {
         MessageBox.Show("Invalid input");
       }
    }