使用复选框控件过滤文本框中指定的字符 [英] Filter Specified Char In Textbox Using Checkbox Control

查看:109
本文介绍了使用复选框控件过滤文本框中指定的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code Project会员..

我有一些代码可以使用此代码使用keypress事件禁用文本框控件中的某些字符:



  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 = ;
}

}
其他
{
}
}





当我选中checkbox1控件时,我希望成功使我的textbox1控件有一个输入只有''a''char 。但我的问题是,当我的目标是使我的textbox1控件可以有双输入时,只有''a''和'b''。



时我检查了checkbox1& checkbox2,我的textbox1控件无法得到我想要的字符..

这是代码..

  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 = ;
}

}
其他
{
}

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

}
else
{
e.Handled = ;
}

}
其他
{
}

}





所以如何让我的textbox1控制只能得到''a''&当我检查复选框时,''b''字符复选框1& checkbox2 control?

解决方案

这很简单。



 < span class =code-keyword> if (checkBox1.Checked&& checkBox2.Checked)
{
// 做你的东西
}
else if (checkBox1.Checked)
{
// 做你的东西
}
else if (checkBox2.Checked)
{
// 做你的东西
}


Hello Code Project Member..
I have a some code to disable some character in textbox control using keypress event with this code :

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..

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?

解决方案

It is very simple.

if (checkBox1.Checked && checkBox2.Checked)
{
    //Do your stuff
}
else if (checkBox1.Checked)
{
    //Do your stuff
}
else if (checkBox2.Checked)
{
    //Do your stuff
}


这篇关于使用复选框控件过滤文本框中指定的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆