在 VB.Net 中使用 Enter 键的 Tab 键功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8406769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:05:23  来源:igfitidea点击:

Tab Key Functionality Using Enter Key in VB.Net

.netvb.netvisual-studio-2010textbox

提问by Kishore Kumar

I have a form with nearly 20 Textbox and 5 Combobox and one control in dependent on the other, Now I want to write the code for the form in such a way that, Pressing EnterKey and TabKey should have the same functionality.

我有一个包含近 20 个文本框和 5 个组合框以及一个依赖于另一个控件的表单,现在我想以这样一种方式编写表单代码,即 Pressing EnterKey 和TabKey 应该具有相同的功能。

Like on pressing TabKey the focus moves to next control should also be performed when I press the EnterKey. Similarly when I press the EnterKey, there is some process code written in key press event but this should also be performed when I press the TabKey.

就像按下TabKey一样,当我按下EnterKey时,也应该执行焦点移动到下一个控件。同样,当我按下EnterKey 时,在按键事件中写入了一些处理代码,但这也应该在我按下TabKey时执行。

回答by Jordon Malik

First Make your Form's Keypreview property= True And Then Paste The Code Below in Form's Keydown Event

首先使您的表单的 Keypreview 属性 = True,然后将下面的代码粘贴到表单的 Keydown 事件中

 If e.KeyCode = Keys.Enter Then
    Me.SelectNextControl(Me.ActiveControl, True, True, True, False) 'for Select Next Control
End If

回答by Mark Hall

The way that I have accomplished it in Winforms is by using the SelectNextControlMethod.

我在 Winforms 中完成它的方式是使用SelectNextControl方法。

i.e.

IE

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    Dim tb As TextBox
    tb = CType(sender, TextBox)

    If Char.IsControl(e.KeyChar) Then
        If e.KeyChar.Equals(Chr(Keys.Return)) Then
            Me.SelectNextControl(tb, True, True, False, True)
            e.Handled = True
        End If
    End If
End Sub

If you are using WPF you can use TraversalRequest

如果您使用的是 WPF,则可以使用 TraversalRequest

i.e.

IE

Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
    Dim tb As TextBox
    tb = CType(sender, TextBox)

    If e.Key = Key.Return Then
        tb.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))
    ElseIf e.Key = Key.Tab Then
        Exit Sub
    End If
End Sub

As far as intercepting the TabKey take a look at this Stackoverflow question.

至于拦截TabKey 看看这个Stackoverflow 问题

回答by Sandesh Mhatre

simply make following function

只需制作以下功能

Public Sub perform_tab_on_enter(ByVal e As KeyEventArgs) 
If e.KeyCode = Keys.Enter Then 
  SendKeys.Send("{TAB}")
else
  exit sub
End If
e.SuppressKeyPress = True 'this will prevent ding sound 
End Sub

call this function on control's keydown event

在控件的 keydown 事件上调用此函数

回答by Atmaweapon

I was able to accomplish this without having to manually create or set event handlers for each control. At the initialization of the form, I run a function that loops through every control and adds a generic handler function.

我无需为每个控件手动创建或设置事件处理程序即可完成此操作。在表单的初始化过程中,我运行了一个循环遍历每个控件并添加一个通用处理函数的函数。

Private Sub AddHandlers()
    Try
        'Get the first control in the tab order.
        Dim ctl As Windows.Forms.Control = Me.GetNextControl(Me, True)
        Do Until ctl Is Nothing
            If TypeOf ctl Is System.Windows.Forms.TextBox Or TypeOf ctl Is System.Windows.Forms.ComboBox _
                    Or TypeOf ctl Is System.Windows.Forms.CheckBox Or TypeOf ctl Is System.Windows.Forms.DateTimePicker Then

                AddHandler ctl.KeyDown, AddressOf ReturnKeyTabs
            End If
            'Get the next control in the tab order.
            ctl = Me.GetNextControl(ctl, True)
        Loop
    Catch ex As Exception
    End Try
End Sub

Private Sub ReturnKeyTabs(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    If e.KeyCode = System.Windows.Forms.Keys.Return Then
        e.Handled = True
        e.SuppressKeyPress = True
    End If
    ReturnKeyTabs(e.KeyCode)
End Sub

Private Sub ReturnKeyTabs(ByVal KeyCode As System.Windows.Forms.Keys)
    If KeyCode = System.Windows.Forms.Keys.Return Then
        System.Windows.Forms.SendKeys.Send("{Tab}")
        KeyCode = 0
    End If
End Sub

回答by J_K_M_A_N

This is quite old but I got here because I wanted to do the same thing. The problem with some of the answers here is that they will always jump to the next control when pressing enter and I only want it to do that with text boxes. If they get to a button, I want them to be able to hit enter to "click" that button. So here is what I did.

这已经很老了,但我来到这里是因为我想做同样的事情。这里的一些答案的问题是,当按下 Enter 键时,它们总是会跳转到下一个控件,而我只希望它对文本框执行此操作。如果他们找到一个按钮,我希望他们能够按 Enter 键“单击”该按钮。所以这就是我所做的。

Private Sub txtName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtName.KeyPress, txtAttn.KeyPress, txtAdd1.KeyPress, txtAdd2.KeyPress, txtCity.KeyPress, txtState.KeyPress, txtZip.KeyPress

    If Asc(e.KeyChar) = 13 Then
        e.Handled = True
        SendKeys.SendWait("{TAB}")
    End If

End Sub

It is kind of a pain having to add all of the .keypress in the handles part of the sub but then you can control which items will cause it to move to the next control and which will not. Of course you also have to set the tab stops order at design time for this to work. But with this method, it can still trigger a button press once it tabs to a button and they hit enter again.

必须在 sub 的手柄部分添加所有 .keypress 是一种痛苦,但是您可以控制哪些项目会导致它移动到下一个控件,哪些不会。当然,您还必须在设计时设置制表位顺序才能使其正常工作。但是使用这种方法,它仍然可以在它切换到一个按钮并再次按下 Enter 时触发按钮按下。

I would have just added this as a comment but I do not have enough points to add comments. :)

我只是将其添加为评论,但我没有足够的积分来添加评论。:)

回答by Shree D

A better option that I uses for the same problem is to create a new textbox class textboxClass and paste following code in its keypress event

我用于同一问题的更好选择是创建一个新的文本框类 textboxClass 并在其按键事件中粘贴以下代码

    Private Sub commonTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            If Char.IsControl(e.KeyChar) Then
                If e.KeyChar.Equals(Chr(Keys.Return)) Then
                    Me.Parent.SelectNextControl(Me, True, True, False, True)
                    e.Handled = True
                End If
            End If
    End Sub

Now we can add any number of textbox to any form. it will behave as desired. when enter is pressed on the last textbox focus goes to first one.

现在我们可以向任何表单添加任意数量的文本框。它将按预期运行。当在最后一个文本框上按下 Enter 时,焦点会转到第一个。

This code as taken from @Mark Hall for single textbox from this page only.

此代码取自@Mark Hall,仅用于此页面的单个文本框。

回答by Robb Woods

You can do it with some javascript: Enter Key Focus

你可以用一些 javascript: Enter Key Focus