可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 Enter Key and Tab Key should have the same functionality.
Like on pressing Tab Key the focus moves to next control should also be performed when I press the Enter Key.
Similarly when I press the Enter Key, there is some process code written in key press event but this should also be performed when I press the Tab Key.
回答1:
The way that I have accomplished it in Winforms is by using the SelectNextControl
Method.
i.e.
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
i.e.
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 Tab Key take a look at this Stackoverflow question.
回答2:
First Make your Form's Keypreview property= True
And Then Paste The Code Below in Form's Keydown Event
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, True, True, True, False) 'for Select Next Control
End If
回答3:
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
回答4:
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
回答5:
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
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.
This code as taken from @Mark Hall for single textbox from this page only.
回答6:
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.
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.
I would have just added this as a comment but I do not have enough points to add comments. :)
回答7:
You can do it with some javascript:
Enter Key Focus