Enter key pressed event handler

后端 未结 5 1300
予麋鹿
予麋鹿 2020-11-30 04:42

I want to capture the text from the textbox when enter key is hit. I am using WPF/visual studio 2010/.NET 4. I dont know what event handler to be used in the tag ? I als

5条回答
  •  半阙折子戏
    2020-11-30 05:08

    For those who struggle at capturing Enter key on TextBox or other input control, if your Form has AcceptButton defined, you will not be able to use KeyDown event to capture Enter.

    What you should do is to catch the Enter key at form level. Add this code to the form:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
        {
            //do something
            return true;
        }
        else
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
    

提交回复
热议问题