在textbox的keypress中抓不得回车,要重载Form的ProcessDialogKey来抓。
比如:(将下面代码拷贝到Form代码中)
protected override bool ProcessDialogKey(Keys keyData)
{ if (keyData == Keys.Return && this.ActiveControl is TextBox)
{
MessageBox.Show("抓到了");
return true;
} base.ProcessDialogKey(keyData);
return false; }
}
base.ProcessDialogKey(aKey);
return false;
}

解决方案 »

  1.   


    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (e.KeyChar==13)
    {
    }
    }
      

  2.   

    TheAres(班门斧) :你的方法的确能抓到,但是所有textbox里都捕捉到这个事件了,怎么能这对多个textbox中的一个抓回车呢
      

  3.   

    不好意思,实际上第一个帖子中的说法并不是很全面,当Form的KeyPreview设置为false的时候,在textbox的keypress中是可以抓到回车的。 private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar == (char)13)
    {
    MessageBox.Show("kkk");
    }

    }当Form的keypreview设置为true的时候,Form就会先抓取。这时候,判断一下this.ActiveControl,转换为textbox,然后判断name或者其他的属性。 protected override bool ProcessDialogKey(Keys keyData)
    { if (keyData == Keys.Return)
    {
    TextBox t = this.ActiveControl as TextBox;
    if (t != null)
    {
    if (t.Name == "textBox1")
    {
    MessageBox.Show("抓到了");
    return true;
    }
    }
    } base.ProcessDialogKey(keyData);
    return false; }