使用箭头键在MouseOver事件上移动文本框?

时间:2018-10-07 20:28:53

标签: vb.net

我正在使用VB 2015,在我的表格中,我有5个TextBox。当我将鼠标悬停在标签上时,我想使用箭头键重新定位这些TextBox。例如,如果用户将鼠标悬停在“ H”标签上,则可以仅使用向左或向右箭头键;如果将鼠标悬停在“ L”标签上,则可以使用向上或向下箭头键。

我可以轻松找到mouseOver事件;

Private Sub lblDateH_MouseEnter(sender As System.Object, e As System.EventArgs) Handles lblDateH.MouseEnter
    MessageBox.Show("You Moused Over")
End Sub

我可以找到箭头键;

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    'detect up arrow key
    If keyData = Keys.Up Then
        MessageBox.Show("You pressed Up arrow key")
        Return True
    End If
    'detect down arrow key
    If keyData = Keys.Down Then
        MessageBox.Show("You pressed Down arrow key")
        Return True
    End If
    'detect left arrow key
    If keyData = Keys.Left Then
        MessageBox.Show("You pressed Left arrow key")
        Return True
    End If
    'detect right arrow key
    If keyData = Keys.Right Then
        MessageBox.Show("You pressed Right arrow key")
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

那么如何同时实现呢?

然后我将使用ApplicationSettingsBase将这些设置存储在窗体关闭中。

2 个答案:

答案 0 :(得分:0)

我想我明白了,这行得通;

If keyData = Keys.Up Then
    DateBox.Location = New Point(DateBox.Location.X, DateBox.Location.Y - 1)
    'MessageBox.Show("You pressed Up arrow key")
    Return True
End If

答案 1 :(得分:0)

完全工作的子控件,它捕获焦点控件的名称并将其传递给子控件,然后使用键来重新定位文本框的名称。设置。设置然后存储新值;

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    'Sub detects which arrow key is pressed
    Dim strControlName As String
    ' Get the name of the control
    strControlName = Me.ActiveControl.Name
    Dim aControl = Me.Controls.Item(strControlName)
    If strControlName <> "PrintButton" Then
        If keyData = Keys.Up Then
            aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
            Return True
        End If
        'detect down arrow ke
        If keyData = Keys.Down Then
            aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
            Return True
        End If
        'detect left arrow key
        If keyData = Keys.Left Then
            aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
            Return True
        End If
        'detect right arrow key
        If keyData = Keys.Right Then
            aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
            Return True
        End If
    End If

    Return MyBase.ProcessCmdKey(msg, keyData)
End Function