How to make Enter on a TextBox act as TAB button

后端 未结 12 2513
迷失自我
迷失自我 2020-12-25 13:10

I\'ve several textboxes. I would like to make the Enter button act as Tab. So that when I will be in one textbox, pressing Enter will move me to the next one. Could you plea

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

    This worked for me

    if (e.Key == Key.Enter)
                ((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
    
    0 讨论(0)
  • 2020-12-25 14:07

    Here is the code that I usually use. It must be on KeyDown event.

    if (e.KeyData == Keys.Enter)
    {
        e.SuppressKeyPress = true;
        SelectNextControl(ActiveControl, true, true, true, true);
    }
    

    UPDATE

    Other way is sending "TAB" key! And overriding the method make it so easier :)

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {            
        if (keyData == (Keys.Enter))
        {
            SendKeys.Send("{TAB}");
        }
    
        return base.ProcessCmdKey(ref msg, keyData);
    }
    
    0 讨论(0)
  • 2020-12-25 14:12

    I would combine what Pharabus and arul answered like this:

    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == ‘\r’)
    {
    e.Handled = true;
    parentForm.GetNextControl().Focus()
    }
    }
    

    Let me know if this helps! JFV

    0 讨论(0)
  • 2020-12-25 14:13

    It is important to note that if you will get an annoying "ding" or warning sound each time that the control is expecting an associated button control and e.Handled = true isn't always the answer to rid yourself of the noise/sound/ding.

    If the control (i.e. single-line textbox) is expecting an associated button to 'ENTER' your entry, then you must also deal with the 'missing' control.

    e.Handled = e.SuppressKeyPress = true;
    

    This may be helpful when getting rid of the 'ding' sound.

    For what it's worth- in my circumstance my users needed to use the "ENTER KEY" as we were transitioning from a terminal/green-screen application to a windows app and they were more used to "ENTER-ing" through fields rather than tabbing.

    All these methods worked but still kept the annoying sound until I added e.SuppressKeyPress.

    0 讨论(0)
  • 2020-12-25 14:13

    I use this code in one of the text box keydown event

    if (e.KeyData == Keys.Enter)
    {
         e.SuppressKeyPress = true;
         SelectNextControl(ActiveControl, true, true, true, true);
    }
    

    Unable handle this keydown event for all text boxes in my form. Suggest something. Thanks

    0 讨论(0)
  • 2020-12-25 14:14

    If you define Tab Order of all controls and make Form.KeyPreview = True, only need this:

        Private Sub frmStart_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            System.Windows.Forms.SendKeys.Send("{TAB}")
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题