My employers have to enter data into two columns and they want the enter key to go one column to the right and then when they press it again to go to the next row in the first column. Is this possible?
Printable View
My employers have to enter data into two columns and they want the enter key to go one column to the right and then when they press it again to go to the next row in the first column. Is this possible?
The effect is possible, before you code it however, you do realize the Tab key does exactly what you want?
Well the problem is that there are four columns in the datagridview but they only need to enter data in the first two columns. Also, they are going to be using a key pad so they want to be able to use the enter key on that.
The following worked for me:
In the code, I assume that the first two columns are the columns that will have data entered into them.Code:Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.DataGridView1.ColumnCount = 4
End Sub
End Class
Public Class DataGridViewEx
Inherits DataGridView
Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Return Me.ProcessEnterKey(keyData)
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
Protected Shadows Function ProcessEnterKey(ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Dim address = MyBase.CurrentCellAddress
If address.X = 0 Then
MyBase.CurrentCell = MyBase.Item(1, address.Y)
ElseIf address.X = 1 Then
Dim newY = address.Y + 1
If MyBase.RowCount > newY Then
MyBase.CurrentCell = MyBase.Item(0, newY)
End If
End If
Return True
End If
Return MyBase.ProcessEnterKey(keyData)
End Function
Protected Overrides Function ProcessDataGridViewKey(ByVal e As KeyEventArgs) As Boolean
If e.KeyCode = Keys.Enter Then
Return Me.ProcessEnterKey(e.KeyData)
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
End Class
wow! thanks so much
ok, so sorry that i replied to this a week later. but i'm having trouble with this. i put the code into my program and it isn't working.
so i have tabs with 20 datagridviews all with multiple columns with lots of formatting. so i'm wondering if there is a way to change the enter key without creating a custom dgv. Is there another way?
anyone know?