這功能就是有時在TextBox打好資料後,懶的用滑鼠點到下方的"確定"按鈕去執行,

而是直接按下Enter跳到下方的"確定"按鈕上面並執行

先點一下TextBox然後產生它的KeyDown事件,在這裡我的TextBox名字是F_Search

"確定"按鈕的名字是btn_Search

下方就是KeyDown事件,只要按下鍵盤,就會觸發下面這個函式

private void F_Search_KeyDown(object sender, KeyEventArgs e)

{  

            //Return可用Keys.Enter替代,這樣寫if(e.KeyCode==Keys.Enter)

            //如果要偵測同時按了AltQ的話,就用if(e.Alt && e.KeyCode==Keys.Q)

            if (e.KeyCode.ToString() == "Return")

            {

                //把游標跳到那個"確定"按鈕上

                btn_Search.Focus();

                //執行"確定"按鈕的Click事件,就會跳出"嗨嗨"

                btn_Search_Click(sender, e);

            }

}

 

//這是"確定"按鈕的Click事件

private void btn_Search_Click(object sender, KeyEventArgs e)

{  

    MessageBox.Show("嗨嗨");      

}

 

arrow
arrow
    全站熱搜

    welkingunther 發表在 痞客邦 留言(1) 人氣()