Focus on toolstrip button vb.net - 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.

Related

Use Left/Right Arrow Keys on Button Click event VS2008

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

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

Cursor inside cell datagridview vb.net

VB.NET DataGridView control:
I know this is probably super simple, but how can I put the blinking cursor inside a specific cell on form load?
I want the effect to be kind of like when you have a form with text boxes and when you set the tab order correctly, the first textbox will automatically have the focus and you can start typing right away.
On a related question: how can I make the enter key activate the form's accept button when you're inside a cell?
Thanks!
You're right, it is fairly easy to position the input cursor to a specific cell in a DataGridView. Set the DataGridView's CurrentCell property to the cell you want the cursor in and then call the DGV's BeginEdit method.
It's also easy to programmatically click (which I believe is what you mean by 'activate') a form's AcceptButton. Call the PerformClick method on the form instance's AcceptButton:
Me.AcceptButton.PerformClick()
Getting the DGV to handle the Enter key is a little trickier even though it might not seem so on the surface. The reason it is tricky is because when you have a DGV cell in edit mode, the DGV KeyPress and PreviewKeyPress events don't fire. The solution is to create your own DataGridView by extending the standard DataGridView and then overriding the ProcessCmdKey function to fire an event that tells listeners the Enter key was pressed.
Here's what your extended DGV control might look like:
Public Class MyDataGridView
Inherits System.Windows.Forms.DataGridView
Public Event EnterKeyPressed()
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Enter Then
RaiseEvent EnterKeyPressed()
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
So assuming you're using the extended DGV above here's how you can do what you need to do:
' This positions the input cursor in the third column in the third row.
MyDataGridView1.CurrentCell = MyDataGridView1.Rows(2).Cells(2)
MyDataGridView1.BeginEdit(False)
' Be sure to wire up the EnterKeyPress event on your shiny new MyDataGridView.
Private Sub MyDataGridView1_EnterKeyPressed() Handles MyDataGridView1.EnterKeyPressed
Me.AcceptButton.PerformClick()
End Sub
Try this code, it will definitely work
dim strRowNumber as string
For Each row As DataGridViewRow In DataGrid.Rows
strRowNumber = DataGrid.Rows.Count - 1
DataGrid.CurrentCell = DataGrid.Rows(strRowNumber).Cells(1)'Cell(1) is a cell nowhich u want to edit or set focus
DataGrid.BeginEdit(False)
Next