就是按下键盘上的某个键(除Enter,Esc键)时触发button1_Click事件

解决方案 »

  1.   

    private void txtname_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (Convert.ToInt32(e.KeyChar) == 13)
    {
    bool bFocus = this.txtpasswd.Focus();
    bFocus = true;
    }
    }
    keypress 事件
      

  2.   

    // 先设置
    this.KeyPreview = true; private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if( e.KeyValue == 27 ) // 如果是按下 Esc 键,触发 Button_Click 事件
    {
    Button_Click( null, null );
    }
    }
      

  3.   

    加如下的代码:
    this.KeyPreview = true;
      

  4.   

    重置
    在定义的变量写下面代码protected override bool ProcessDialogKey(Keys keyData)
    {
    if (keyData==Keys.F1)
    {
    确定_ButtonClick(null,null);
    }
    if (keyData==Keys.Escape)
    {
    toolBar3_ButtonClick(null,null);
    }
    if (keyData==Keys.F9)
    {
    checkBox1.Checked=!checkBox1.Checked;
    }
    return base.ProcessDialogKey (keyData);
    }