Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hello Code Project Member..
I have a some code to disable some character in textbox control using keypress event with this code :

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (checkBox1.Checked == true)
            {
                string asciiCode = Convert.ToString(e.KeyChar);
                if ((asciiCode == "a" || ((e.KeyChar == '\b'))))
                {

                }
                else
                {
                    e.Handled = true;
                }

            }
            else
            {
            }
}


and when i am checked checkbox1 control, my desire success to make my textbox1 control can have one input there is only 'a' char. but my problem is come when my goal is to make my textbox1 control can have double input there is only 'a' and 'b'.

when i checked checkbox1 & checkbox2 , my textbox1 control can't get my desire char..
this is the code..
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (checkBox1.Checked == true)
          {
              string asciiCode = Convert.ToString(e.KeyChar);
              if ((asciiCode == "a" || ((e.KeyChar == '\b'))))
              {

              }
              else
              {
                  e.Handled = true;
              }

          }
          else
          {
          }

          if (checkBox2.Checked == true)
          {
              string asciiCode = Convert.ToString(e.KeyChar);
              if ((asciiCode == "b" || ((e.KeyChar == '\b'))))
              {

              }
              else
              {
                  e.Handled = true;
              }

          }
          else
          {
          }

      }


so how to make my textbox1 control only can get 'a' & 'b' char when iam checked checkbox1 & checkbox2 control?
Posted

1 solution

It is very simple.

C#
if (checkBox1.Checked && checkBox2.Checked)
{
    //Do your stuff
}
else if (checkBox1.Checked)
{
    //Do your stuff
}
else if (checkBox2.Checked)
{
    //Do your stuff
}
 
Share this answer
 
Comments
Gun Gun Febrianza 18-May-13 1:46am    
haaaaaaa (y)
thank you i will do it sir.. :)
i will report my result soon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900