Results 1 to 8 of 8

Thread: Changing what the enter key does in Datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Changing what the enter key does in Datagridview

    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?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Changing what the enter key does in Datagridview

    The effect is possible, before you code it however, you do realize the Tab key does exactly what you want?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Re: Changing what the enter key does in Datagridview

    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.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Changing what the enter key does in Datagridview

    The following worked for me:
    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
    In the code, I assume that the first two columns are the columns that will have data entered into them.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Re: Changing what the enter key does in Datagridview

    wow! thanks so much

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Re: Changing what the enter key does in Datagridview

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Re: Changing what the enter key does in Datagridview

    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?
    Last edited by barnaby; Aug 15th, 2011 at 11:13 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    70

    Re: Changing what the enter key does in Datagridview

    anyone know?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width