Use Left/Right Arrow Keys on Button Click event VS2008 - vb.net

I am trying to use the left and right arrow keys to do performclick on my back and next buttons. Here's the code:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Left Then
Me.cmdPrevQ.PerformClick()
Return True
End If
If keyData = Keys.Right Then
Me.cmdNextQ.PerformClick()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I want this function to only work as soon as I click on a listview item and not on formload but I don't know how to do that :(
I tried the KeyDown event for my listview but what happens is even if I haven't clicked on anything in my listview and I press on the left or right arrow keys, it gives me an error as my next and back buttons rely on the items inside my listview.
Please help

Related

How can I handle Enter Key Press in DataGridView when the Cell is in EditMode?

I have a DataGridView and I have made AllowUserToAddRows property to false and in Load event handler of form, I added a new row to the grid using a function. Now, when user edits the datagidview and once he ends edit and presses Enter I want to call that function to add a new row to grid again.
I tried KeyDown event of DataGridView, but it seems the event will not trigger when the cell is in edit mode.
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If msg.WParam.ToInt32() = Keys.Enter Then
DataGridView1.EndEdit()
MessageBox.Show("Enter key is pressed")
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Use this function with your condition
Microsoft says that you can't add a new row to a databound DataGridView but that is not true.
I assume that you want add a new row to the end of the grid. By default the DataGridView control has an empty row with the index -1 at the bottom of the grid. To add a new row programmatically do this:
Dim rowindex as Int32
rowIndex =
DataGridView1.Rows.GetLastRow(includeFilter:=0)
DataGridView1.CurrentCell =
DataGridView1.Rows.Item(rowIndex).Cells(1)
DataGridView1.BeginEdit(selectAll:=True)
This gives you a new row. If you set the DataGridView to EditOnEnter you will see the pen icon on the far left when you click on a cell.

KeyDown Event Key Not Work - VB.net

KeyDown event does not work, pressing escape the form does not close
Private Sub DataTable_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub
Well, sure it works, the event is just not very like to fire. Keystrokes raise the KeyDown event on the control with the focus. That will only ever be your form when it has no controls that can get the focus. A fairly unlikely scenario.
If you already have a Button labeled "Cancel" that closes the form then set the form's CancelButton property.
If you don't have such a button then it gets to be pretty unlikely that the user will figure out by himself that the Escape key is useful. He will most likely use the Close button in the upper right corner. You can nevertheless make it work by overriding the ProcessCmdKey() method. Like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Escape Then
Me.Close()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

TabControl shortcut key in vb.net

I have a tab control on my form. I would like to add a shortcut key for each page, so it could be used without a mouse or multiple tab key presses.
I can't find any property in controltabs properties to do this. I have tried to use the &-sign in the Text property of tabPage, but it doesn't work.
You can use tabControl1.SelectTab(i) in the KeyDown event of the TabControl to move from one page to another. I'm not sure how to get an underscore in the tabpage label.
You can also use a command box on each tab page to go from one page to the next.
Add code like this to your Form, then modify accordingly:
Public Class Form1
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case Keys.F1
TabControl1.SelectedIndex = 0
Return True
Case Keys.F2
TabControl1.SelectedIndex = 1
Return True
Case Keys.F3
TabControl1.SelectedIndex = 2
Return True
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

Set Focus to a text box when F1 is pressed

I have been trying to get this to work but it's doing my head in, wondered if you experts can help me out.
On my form I would like to set focus to a TextBox when I press F1 on the keyboard, I have the code written but somehow it does not work when I press F1. What am I doing wrong? I have also set keypreview to true.
The code here:
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyData = Keys.F1 Then
txtemployeeno.Focus()
End If
End Sub
The problem is that your KeyUp event isn't firing because the form doesn't technically have input focus (though it may be activated). If you wish to use the KeyPreview property, you need to use the KeyPress event instead of KeyUp.
Alternatively, you could always override the ProcessCmdKey function. Just add the following method to your form's code:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.F1 Then
txtemployeeno.Focus()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Focus on toolstrip button vb.net

I created a toolstrip with some buttons. I have a form with some objects (like textbox,checkbox,button etc.) and when i get the focus on last item,when i finish to compile i need with tab on keyboard to get focus on save button on the toolstrip.
Can anyone tell me how to do that? Because there isn't the focus option on toolstrip.
Thanks.
You'll need to override the Tab key handling. Say your last control is Button3 and the toolstrip button is SaveButton then the code looks like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Tab AndAlso Me.ActiveControl Is Button3 Then
ToolStrip1.Focus()
SaveButton.Select()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Adjust as necessary to work with your specific controls.