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?
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 = 1End 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.
Here is one more thing I forgot to add. In order to exclude control characters, you can add a simple check in the initial IF statement, like this:
VB:
If
Not Char.IsControl(e.KeyChar) And Not grid Is Nothing And Not grid.ActiveCell Is Nothing And TypeOf grid.ActiveCell.EditorResolved Is EditorWithText And grid.ActiveCell.IsInEditMode = False
Then
C#
if
(!Char.IsControl(e.KeyChar) && grid != null && grid.ActiveCell != null && grid.ActiveCell.EditorResolved is EditorWithText && !grid.ActiveCell.IsInEditMode)
I have added a VS 2005 VB project here for you to download and test. Please let me know if you have any other questions.
I tried the same but there are way too many side effects of this implementation.
When someone tries to enter Shift +a for caps, it prints shift. Same with the use of other non alphanumeric keys.
Did anyone figure out a definitive way to achieve this?
I just tested Charlie's sample by clicking a cell and pressing Shift+a and it works just fine for me. The cell goes into edit mode when I press the "a", and it shows "A", just like it should.