如何使TextBox上的Enter作为TAB按钮

时间:2022-08-23 22:53:40

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 please tell me how to implement this approach without adding any code inside textbox class (no override and so on if possible)?

我有几个文本框。我想让Enter按钮充当Tab。因此,当我将在一个文本框中时,按Enter将使我移动到下一个文本框。你能告诉我如何实现这种方法而不在textbox类中添加任何代码(如果可能的话,没有覆盖等等)?

11 个解决方案

#1


18  

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

这是我经常使用的代码。它必须是KeyDown事件。

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

UPDATE

UPDATE

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

其他方式是发送“TAB”键!并覆盖该方法使它更容易:)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{            
    if (keyData == (Keys.Enter))
    {
        SendKeys.Send("{TAB}");
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

#2


10  

You can write on the keyDown of any control:

你可以写任何控件的keyDown:

        if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();

            }
        }

GetNextControl doesn't work on Vista.

GetNextControl不适用于Vista。

To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:

要使它适用于Vista,您需要使用下面的代码来替换this.GetNextControl ...:

System.Windows.Forms.SendKeys.Send("{TAB}");

#3


4  

You don't need to make an "enter event handler"

您不需要创建“输入事件处理程序”

All you need to do is make a "central" KeyDown event:

您需要做的就是制作一个“中心”KeyDown事件:

example

private void General_KeyDown(object sender, KeyPressEventArgs e)
 {
 if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();
            }
        }
}

Then all you have to do is go to designer select all textboxes you wish to cycle through with EnterKey (select them by holding down Ctrl and clicking on textbox with the mouse) then go to Events(thunder like button), search Keydown event and type inside General_KeyDown. Now all your selected Textboxes will have the same keydown event :) This makes everything muuuuch much easier, cause imagine a form with 100 textboxes and you want to cycle through all with enter.... making an apart event for each texbox is... well not a proper way to make a program, it ain't neat. Hope it helped!!

然后你要做的就是去设计师选择你想用EnterKey循环的所有文本框(通过按住Ctrl并用鼠标点击文本框来选择它们)然后转到事件(雷声像按钮),搜索Keydown事件和类型在General_KeyDown里面。现在你所选择的所有文本框都将具有相同的keydown事件:)这使得所有内容都变得更加容易,因此想象一个包含100个文本框的表单,并且您希望使用enter循环遍历....为每个texbox创建一个单独的事件是...好吧,不是一个制作节目的正确方法,它不是很整洁。希望它有所帮助!!

Blockquote

大段引用

#4


2  

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.

重要的是要注意,如果每次控件期望相关的按钮控制并且e.Handled = true时,你会得到恼人的“叮”声或警告声并不总是能够摆脱噪音/声音的答案/丁。

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.

在我的情况下,我的用户需要使用“ENTER KEY”,因为我们正在从终端/绿屏应用程序转换到Windows应用程序,他们更习惯于通过字段而不是标签来“输入” 。

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

所有这些方法都有效但在我添加e.SuppressKeyPress之前仍然保持令人讨厌的声音。

#5


1  

This worked for me

这对我有用

if (e.Key == Key.Enter)
            ((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));

#6


1  

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

如果您定义所有控件的Tab Order并使Form.KeyPreview = True,则只需要:

    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

#7


0  

For those of you that code in vb...

对于那些在vb中编码的人...

Public Class NoReturnTextBox
    Inherits System.Windows.Forms.TextBox

    Const CARRIAGE_RETURN As Char = Chr(13)

    ' Trap for return key....
    Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar = CARRIAGE_RETURN Then
            e.Handled = True
            System.Windows.Forms.SendKeys.Send(vbTab)
        End If

    End Sub

End Class

#8


0  

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

我在其中一个文本框keydown事件中使用此代码

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

无法处理表单中所有文本框的keydown事件。建议一些事情。谢谢

#9


0  

This is the solution I use for VB.NET

这是我用于VB.NET的解决方案

  1. Set Keypreview=True in your form properties.

    在表单属性中设置Keypreview = True。

  2. Put this code in form keydown event:

    将此代码放在表单keydown事件中:

    If (e.KeyData = Keys.Enter) Then

    如果(e.KeyData = Keys.Enter)那么

    'for any multiline control, you have to exit to let multiline 'textbox intro 'keypressing makes line skips.

    '对于任何多行控件,你必须退出让多行'文本框介绍'按键才能使行跳过。

     If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub 
    
     e.SuppressKeyPress = True
     SelectNextControl(ActiveControl, True, True, True, True)
    

    End If

    万一

Enjoy !!!!

请享用 !!!!

Xabier Aberasturi Larruzea

Xabier Aberasturi Larruzea

#10


-1  

Taking a wild guess:

猜测一下:

// on enter event handler
parentForm.GetNextControl().Focus();

#11


-1  

I would combine what Pharabus and arul answered like this:

我会把Pharabus和arul的回答结合起来:

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

如果这有帮助,请告诉我! JFV

#1


18  

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

这是我经常使用的代码。它必须是KeyDown事件。

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

UPDATE

UPDATE

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

其他方式是发送“TAB”键!并覆盖该方法使它更容易:)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{            
    if (keyData == (Keys.Enter))
    {
        SendKeys.Send("{TAB}");
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

#2


10  

You can write on the keyDown of any control:

你可以写任何控件的keyDown:

        if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();

            }
        }

GetNextControl doesn't work on Vista.

GetNextControl不适用于Vista。

To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:

要使它适用于Vista,您需要使用下面的代码来替换this.GetNextControl ...:

System.Windows.Forms.SendKeys.Send("{TAB}");

#3


4  

You don't need to make an "enter event handler"

您不需要创建“输入事件处理程序”

All you need to do is make a "central" KeyDown event:

您需要做的就是制作一个“中心”KeyDown事件:

example

private void General_KeyDown(object sender, KeyPressEventArgs e)
 {
 if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();
            }
        }
}

Then all you have to do is go to designer select all textboxes you wish to cycle through with EnterKey (select them by holding down Ctrl and clicking on textbox with the mouse) then go to Events(thunder like button), search Keydown event and type inside General_KeyDown. Now all your selected Textboxes will have the same keydown event :) This makes everything muuuuch much easier, cause imagine a form with 100 textboxes and you want to cycle through all with enter.... making an apart event for each texbox is... well not a proper way to make a program, it ain't neat. Hope it helped!!

然后你要做的就是去设计师选择你想用EnterKey循环的所有文本框(通过按住Ctrl并用鼠标点击文本框来选择它们)然后转到事件(雷声像按钮),搜索Keydown事件和类型在General_KeyDown里面。现在你所选择的所有文本框都将具有相同的keydown事件:)这使得所有内容都变得更加容易,因此想象一个包含100个文本框的表单,并且您希望使用enter循环遍历....为每个texbox创建一个单独的事件是...好吧,不是一个制作节目的正确方法,它不是很整洁。希望它有所帮助!!

Blockquote

大段引用

#4


2  

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.

重要的是要注意,如果每次控件期望相关的按钮控制并且e.Handled = true时,你会得到恼人的“叮”声或警告声并不总是能够摆脱噪音/声音的答案/丁。

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.

在我的情况下,我的用户需要使用“ENTER KEY”,因为我们正在从终端/绿屏应用程序转换到Windows应用程序,他们更习惯于通过字段而不是标签来“输入” 。

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

所有这些方法都有效但在我添加e.SuppressKeyPress之前仍然保持令人讨厌的声音。

#5


1  

This worked for me

这对我有用

if (e.Key == Key.Enter)
            ((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));

#6


1  

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

如果您定义所有控件的Tab Order并使Form.KeyPreview = True,则只需要:

    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

#7


0  

For those of you that code in vb...

对于那些在vb中编码的人...

Public Class NoReturnTextBox
    Inherits System.Windows.Forms.TextBox

    Const CARRIAGE_RETURN As Char = Chr(13)

    ' Trap for return key....
    Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar = CARRIAGE_RETURN Then
            e.Handled = True
            System.Windows.Forms.SendKeys.Send(vbTab)
        End If

    End Sub

End Class

#8


0  

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

我在其中一个文本框keydown事件中使用此代码

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

无法处理表单中所有文本框的keydown事件。建议一些事情。谢谢

#9


0  

This is the solution I use for VB.NET

这是我用于VB.NET的解决方案

  1. Set Keypreview=True in your form properties.

    在表单属性中设置Keypreview = True。

  2. Put this code in form keydown event:

    将此代码放在表单keydown事件中:

    If (e.KeyData = Keys.Enter) Then

    如果(e.KeyData = Keys.Enter)那么

    'for any multiline control, you have to exit to let multiline 'textbox intro 'keypressing makes line skips.

    '对于任何多行控件,你必须退出让多行'文本框介绍'按键才能使行跳过。

     If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub 
    
     e.SuppressKeyPress = True
     SelectNextControl(ActiveControl, True, True, True, True)
    

    End If

    万一

Enjoy !!!!

请享用 !!!!

Xabier Aberasturi Larruzea

Xabier Aberasturi Larruzea

#10


-1  

Taking a wild guess:

猜测一下:

// on enter event handler
parentForm.GetNextControl().Focus();

#11


-1  

I would combine what Pharabus and arul answered like this:

我会把Pharabus和arul的回答结合起来:

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

如果这有帮助,请告诉我! JFV