Enter key press in C#

前端 未结 14 716
臣服心动
臣服心动 2020-12-01 06:06

I tried this code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Convert.ToInt32(e.KeyChar) == 13) 
    {
        MessageBox.S         


        
相关标签:
14条回答
  • 2020-12-01 06:30

    Try this:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
            switch (e.Key.ToString())
            {
                    case "Return":
                            MessageBox.Show(" Enter pressed ");
                            break;
             }
    }
    
    0 讨论(0)
  • 2020-12-01 06:32

    You must try this in keydown event

    here is the code for that :

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                MessageBox.Show("Enter pressed");
            }
        }
    

    Update :

    Also you can do this with keypress event.

    Try This :

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Return))
            {
                MessageBox.Show("Key pressed");
            }
        }
    
    0 讨论(0)
  • 2020-12-01 06:33
    private void Input_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                MessageBox.Show("Enter pressed");
            }
        }
    

    This worked for me.

    0 讨论(0)
  • Keeping in mind that

    Keys.Return is different to Keys.Enter

    //Makes it easier to Type user and password and press Enter, 
    //Rather than using the mouse to Click the Button all the time
        private void Txt_Password_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Return)
            {
                Btn_Login_Click(null, null);
            }
        }
    

    Hope this help someone.

    0 讨论(0)
  • 2020-12-01 06:41

    Also you can do this with keypress event.

     private void textBox1_EnterKeyPress(object sender, KeyEventArgs e)
    
     {
    
      if (e.KeyCode == Keys.Enter)
       {
         // some code what you wanna do
       }
    
    
    }
    
    0 讨论(0)
  • 2020-12-01 06:42
    private void Ctrl_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            this.SelectNextControl((Control)sender, true, true, true, true);
        }
    }
    

    Select all required textboxes; type in new Events name Ctrl_KeyUp.

    See YouTube by BTNT TV: https://www.bing.com/videos/searchq=c%23+how+to+move+to+next+textbox+after+enterkey&view=detail&mid=999E8CAFC15140E867C0999E8CAFC15140E867C0&FORM=VIRE

    0 讨论(0)
提交回复
热议问题