Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
615
Programming The KeyDown Event in UltraGrid
posted

I am using Visual Studio 2008, VB.NET, Build 2059, and Windows XP.  I have a question about programming the KeyDown event in the UltraGrid so that it behaves like MS-Excel.  I want UltraGrid to go into Edit Mode when the user starts typing (similar to Microsoft Excel and the native .NET DataGridView control.  Here's my current KeyDown event:

            Select Case e.KeyCode
                Case Keys.Tab
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.NextCell, False, False)
                    e.Handled = True
                Case Keys.Enter
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.NextRow, False, False)
                    e.Handled = True
                Case Keys.Up
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.AboveCell, False, False)
                    e.Handled = True
                Case Keys.Down
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.BelowCell, False, False)
                    e.Handled = True
                Case Keys.PageDown
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.PageDownCell, False, False)
                    e.Handled = True
                Case Keys.PageUp
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.PageUpCell, False, False)
                    e.Handled = True
                Case Keys.Home
                    UltraGrid1.Selected.Cells.Clear()
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.FirstCellInRow, False, False)
                    If UltraGrid1.ActiveCell.Selected = False Then UltraGrid1.PerformAction(UltraGridAction.ToggleCellSel)
                Case Keys.End
                    UltraGrid1.Selected.Cells.Clear()
                    UltraGrid1.PerformAction(UltraGridAction.ExitEditMode, False, False)
                    UltraGrid1.PerformAction(UltraGridAction.LastCellInRow, False, False)
                    If UltraGrid1.ActiveCell.Selected = False Then UltraGrid1.PerformAction(UltraGridAction.ToggleCellSel)
                Case Else
                    If UltraGrid1.ActiveCell IsNot Nothing AndAlso (Not UltraGrid1.ActiveCell.IsInEditMode) Then
                        UltraGrid1.PerformAction(UltraGridAction.EnterEditMode, False, False)
                    End If
            End Select

    Please look at the code in the CASE ELSE section.  Two problems.  First, how do I program it so that it ignores keys such as the Function Keys, BackSpace, other control keys etc.?  Second, there is a timing problem with this code.  When I type the letter "M", the first time I press the "M" key, it is lost while it is going into Edit Mode.  Then I have to press "M" a second time to actually see it show up.

     How can I modify the KeyDown event to handle these two issues?

Parents
  • 2197
    posted

    Hello.

    You can accomplish Excel style editing by handling the KeyPress event of the grid and setting the DisplayLayout.Override.CellClickAction to CellSelect. This will ensure that a sinlge click of the cell only selects it; it will not be placed into edit mode yet. To enable instant editing as soon as a key is pressed, handle the grids KeyPress event and place the following code inside:

    Dim grid As UltraGrid = TryCast(sender, UltraGrid)
    If Not grid Is Nothing And Not grid.ActiveCell Is Nothing And TypeOf grid.ActiveCell.EditorResolved Is EditorWithText And grid.ActiveCell.IsInEditMode = False Then

     grid.PerformAction(UltraGridAction.EnterEditMode)
     Dim editor As EditorWithText = CType(grid.ActiveCell.EditorResolved, EditorWithText)
     editor.TextBox.Text = e.KeyChar.ToString()
     editor.TextBox.SelectionStart = 1
    End If

    Now, this code will only work with a cell that uses an EditorWithText, which is what string and numeric columns use by default. You can add extra logic to handle columns of type DateTime and Boolean. Please let us know how this works for you and if you have any other questions, just ask.

Reply Children