我用“+”和回车键作为KeyDown事件的响应键,分别执行不同的功能,当在bianhao文本框中输入“+”时,执行代码后将bianhao清空,但我用下面的代码时bianhao文本框中保留了“+”,而没有被完全清空,请教这问题该怎么解决?
       private void bianhao_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Add)
            {
                …… 
                bianhao.Text = "";
            }

解决方案 »

  1.   

    KeyPress事件里可以,刚试过了
      

  2.   

    刚才不对是像lz说的再去try try
      

  3.   

    那谁能帮我写这个代码呢?我以前都没用过KeyPress,我先在这谢过了!
    this.bianhao.KeyPress += new System.Windows.Forms.KeyEventHandler(this.bianhao_KeyPress);

    private void bianhao_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      

  4.   

    用TextChanged事件代码如下:
    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                string str = this.textBox1.Text;
                if (str.Length > 0)
                {
                    char[] c = str.ToCharArray(str.Length - 1, 1);
                    //char c = str.Substring(str.Length - 2, 1);
                    if (c[0] == '+')
                    {
                        this.textBox1.Text = "";
                    }
                }
            }可以的
      

  5.   

    KeyDown是可以的,你在
    bianhao.Text = "";
    之后加上
    e.Handled = true;
    即可
      

  6.   

    加上e.Handled = true; 这方法试了不行
      

  7.   

    自己做一个,重载ProcessCmdKey函数进行处理,大致如下:
    public class MyTextBox:TextBox
    {
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if( keyData == Keys.Add )
    {
    this.Text = "";
    return true;
    }
    return base.ProcessCmdKey (ref msg, keyData);
    }
    }
      

  8.   

    这个是可以的,在keyPresss事件里,只是我没有小键盘,不知小键盘上的那个“+"是否可以,你自己测试一下
    private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (e.KeyChar =='+')
    {
                   
    this.textBox3.Text = "";
    e.Handled=true;
    }
    }