How to navigate (Edit) between Cells in ListBox?

I wanted to do just that:
I open a text file and read its contents / fill a ListBox.

So far, so good, but I also want to be able to modify a Cell Content (if / when needed).

So, I subclassed a ListBox and add:

Function KeyDown(Key As String) Handles KeyDown as Boolean
  If Key = Chr(3) Or Key = Chr(13) Then
    Me.EditCellAt(Me.SelectedRowIndex,Edit_Col)
    
    Return True
  End If
  If Key = Chr(9) Then
    If Edit_Col < Me.ColumnCount - 1 Then
      // Edit the Next Column…
      Edit_Col = Edit_Col + 1
      Me.EditCellAt(Me.SelectedRowIndex,Edit_Col)
      
      Return True
    Else
      // Edit the first Column…
      Edit_Col = 0
      Me.EditCellAt(Me.SelectedRowIndex,Edit_Col)
      
      Return True
    End If
  End If
End Function

`Public Property Edit_Col As Integer`



Function CellClick(row as Integer, column as Integer, x as Integer, y as Integer) Handles CellClick as Boolean
  Me.CellTypeAt(Row, Column) = ListBox.CellTypes.TextField
End Function

The result is strange… when I press the Tab key:
Click in a Row: Pass the Clicked Cell in Edit mode
Press the Tab Key: The Cell lost Focus
Press the Tab Key: The next Cell get the Focus in Edit Mode…

NB: this is a work in progress (no Shift-Tab for example).

What must I add (or do) to avoid the “Cell Lost Focus” behavior ?

Do you want to move to another cell while you’re already editing one? In this case, you should use the CellKeyDown event (triggered while a cell has the focus).
KeyDown is for when no cell is being edited.

BTW: this block:

… can be replaced by:

Edit_Col = (Edit_Col + 1) mod Me.ColumnCount
me.EditCellAt(me.SelectedRowIndex,Edit_Col)

That’s it !!!

Thank you.

PS: far better than what I wrote earlier…

1 Like