Jump to editable listbox cells with tab key

I have a listbox with some fields that can be changed.
I want to jump from one editable cell to the other (in the same row) with the tab key, eg one doesn’t not want to change anything.
Now: I haven’t fond the right way yet… Do I need to handle the keydown event (in the listbox, or window, where I also scan for a cmd-. event?), has the tab stop of the listbox to be on, any method needs to return true, etc.?
Also, wenn I set a cell to type normal again, sometimes the edit-frame stays around that cell, though wit not possibility to edit as desired. Is this normal, does it need an invalidate or similar?
Any advice, any experience of doing this? Thanks!

I used a subclass of Listbox to do this. It isn’t really hard but you have to accept that you may not be able to use TAB to exit the listbox. My subclass has a a few properties:

Public Property mCurrentColumn as Integer = 0 Public Property mEditMode as Boolean = False

In the CellClick Event handler:

[code]Function CellClick(row as Integer, column as Integer, x as Integer, y as Integer) Handles CellClick as Boolean
#Pragma Unused x
#pragma Unused y

mCurrentColumn = column

// Only enter edit mode if not a Contextual Click
If IsContextualClick = False Then
mEditMode = True
Me.EditCellAt (row, column)
End If

End Function
[/code]

And in the CellKeyDown Event Handler:

[code]Function CellKeyDown(row as Integer, column as Integer, key as String) Handles CellKeyDown as Boolean
#If TargetWindows
#Pragma Unused column
#EndIf

// If the user TABs or SHIFT-TABs move to the next or previous cell
// being sure to watch for outofbounds errors.
Dim i As Integer

i = key.AscByte 'Asc (key)
If i = 9 Then // TAB key
If Keyboard.ShiftKey = False Then
mCurrentColumn = mCurrentColumn + 1
If mCurrentColumn = Me.ColumnCount Then mCurrentColumn = mCurrentColumn -1
mEditMode = True
Me.EditCellAt (row, mCurrentColumn)
Return True
Else
mCurrentColumn = mCurrentColumn - 1
If mCurrentColumn < 0 Then mCurrentColumn = 0
mEditMode = True
Me.EditCellAt (row, mCurrentColumn)
Return True
End If
End If

If (i = 13) OR (i = 3) Then
mEditMode = False
End If

Return False
End Function[/code]
This all allows the user to tab and back-tab within the row. Editing a cell ends when the user hits tab to move within the row or on Enter to accept the entire row. As for skipping non-editable cells, just check the row & column and don’t call EditCell on them.

I’m sure there are other, cleaner ways to do it but this works for me.

Hi Dale

I have it quite similar, but it doesn’t work perfectly so far…

First, in the CellClick function would be a mCurrentColumn = Column required, isn’t it?
Next, is a listbox Tab Stop required or not? Because, after entering the first Tab Key in my application, the listbox gets the blue focus frame, only a second tab will get into the Cell KeyDown function… on the other hand, without it hitting the tab key, the cells never advance…?!
Last, the return true is need, as I see. Just in the KeyDown function, right?

To explain my application a bit more: when hitting a row, it is just selected, e.g. for deleting. If hitting the ‘Edit’ a push button, the first allowed cell can be edited then, using tab the next a.s.o., until hitting the ‘Save’ push button, and the row loses the selection.

Thanks, Peter

[quote=473663:@Peter Kronenberg]Hi Dale

I have it quite similar, but it doesn’t work perfectly so far…

First, in the CellClick function would be a mCurrentColumn = Column required, isn’t it?[/quote]
Yes it is needed since the control needs to track which column is the current column so that the cursor arrows work properly when not in Edit mode.

If you want to TAB into the listbox, then yes. You can set the selected row index in the GotFocus event of the one local to the window, not the class object itself.

Correct. In the CellClick event I want the system to do whatever it needs to do after I’m done with the event so I let it return False.

Ok, but it seems counter-intuitive to me that the user has to move to the cell and hit an Edit button. The code I listed should let the user just click in the cell to enter edit mode. You can block that for certain cells or columns by setting it up in the CellClick event.

You’re welcome.

You might want to also take a look at this example, which demonstrates how to do a grid-type ListBox:

Examples/Desktop/Controls/ListBox/ListBoxGridExample