.NET TextBox - Handling the Enter Key

前端 未结 4 781
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 16:32

What is definitively the best way of performing an action based on the user\'s input of the Enter key (Keys.Enter) in a .NET TextBox, assuming owne

4条回答
  •  萌比男神i
    2020-12-03 17:18

    Inorder to link the function with the key press event of the textbox add the following code in the designer.cs of the form:

     this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
    

    Now define the function 'OnKeyDownHandler' in the cs file of the same form:

    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
    
        if (e.KeyCode == Keys.Enter)
        {
           //enter key has been pressed
           // add your code
        }
    
    }
    

提交回复
热议问题