Reputation: 41
I am working with VB 2015, and in my form I have 5 TextBox. I would like to reposition these TextBox using the arrowkeys when I mouseOver a label. For example if the user mousesOver a "H" label, he can then use just the Left or Right arrow keys, if they mouseOver a "L" label, they can use the up or down arrow keys.
I can easily find a mouseOver event;
Private Sub lblDateH_MouseEnter(sender As System.Object, e As System.EventArgs) Handles lblDateH.MouseEnter
MessageBox.Show("You Moused Over")
End Sub
And I can find arrow keys;
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
So how can implement both?
I will then be using the ApplicationSettingsBase to store these settings on form close.
Upvotes: 0
Views: 187
Reputation: 41
Full working sub, this captures the focused controls name and passes it to the sub, it then uses the keys to relocate the textBox's. Settings.Settings then stores the new values;
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
Upvotes: 0
Reputation: 41
I think I got it, this works;
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
Upvotes: 0