vb.net Enter 和 Tab 将焦点发送到下一个文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34656503/
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
Enter and Tab sends focus to next textbox
提问by bonny
I had made a class by inheriting the system textbox shown in following code.
我通过继承以下代码中显示的系统文本框创建了一个类。
Public Class textboxex
Inherits TextBox
Private Sub TextBoxEx_Return(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
SendKeys.Send("{TAB}")
Me.Text = Me.Text.ToUpper 'To change the text to upper case when it leaves focus.(working fine)
End If
End Sub
Now problem is when I press Tab key, it doesn't enter the if condition.
Probably it would not because I havn't given if condition for tab key.
现在的问题是当我按下 Tab 键时,它没有进入 if 条件。
可能不会,因为我没有给出 tab 键的 if 条件。
But I after I changed the if condition by adding e.keycode = keys.Tab and pressing tab key, it won't do the Uppercase of the letters but enter does it fine. The updated code shown below.
但是我通过添加 e.keycode = keys.Tab 并按 Tab 键更改了 if 条件后,它不会执行字母的大写,但输入就可以了。更新后的代码如下所示。
If e.KeyCode = Keys.Enter or e.KeyCode = Keys.Tab Then
SendKeys.Send("{TAB}")
Me.Text = Me.Text.ToUpper 'doesn't work when tab is pressed | enter works fine
End If
So this is my problem, help me plox...!!!!!!!
所以这是我的问题,帮我 plox...!!!!!!!!!
回答by Reza Aghaei
To make Enterwork like Tab
使Enter工作像Tab
You can override ProcessCmdKeymethod and check if the key is Enterthen, send a Tab key or ussing SelectNextControl, move focus to next control:
您可以覆盖ProcessCmdKey方法并检查键是否存在,Enter然后发送 Tab 键或使用SelectNextControl,将焦点移动到下一个控件:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) _
As Boolean
If (keyData = Keys.Enter) Then
SendKeys.Send("{TAB}")
'Parent.SelectNextControl(Me, True, True, True, True)
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

