简体   繁体   English

Enter和Tab将焦点发送到下一个文本框

[英]Enter and Tab sends focus to next textbox

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. 现在的问题是,当我按Tab键时,它没有输入if条件。
Probably it would not because I havn't given if condition for tab key. 可能不是因为我没有给出Tab键的条件。

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. 但是我在通过添加e.keycode = keys.Tab并按Tab键更改了if条件之后,它不会执行字母的大写字母,但输入就可以了。 The updated code shown below. 更新的代码如下所示。

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...!!!!!!! 所以这是我的问题,请帮助我... !!!!!!!

To make Enter work like Tab 使EnterTab一样工作

You can override ProcessCmdKey method and check if the key is Enter then, 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM