The code below is (obviously) in the CellKeyDown of a ListBox. It works fine (once there is a Cell in Edit mode - cursor inside - several Tab key press moves the cursor in the next Cell [Next Column] and goes to the first column when the cursor is in the last column.
But I have one feature to add and one annoyance:
a. Feature to add:
How do I code a Shift-Tab feature: go to the previous column Or/And to the last once I reach Column(0) ?
b. Annoyance:
I lost the focus; I put before the end of the if
block a Return True
line.
[code]Function CellKeyDown(row as Integer, column as Integer, key as String) As Boolean
// Trap only the Tab character
If Key = Chr(9) Then
If Column < (LB.ColumnCount - 1) Then
// The last Column is accessible, so add 1
Column = Column + 1
// Edit that Cell
Me.EditCell(Row, Column)
Else
// Last column reached, so go to Column(0)
Column = 0
// Edit that Cell
Me.EditCell(Row, Column)
End If
// I handled the Tab character
Return True
End If
End Function[/code]
BTW: in my search quest, I saw code like ascb(Key)
in the If test line: why ?
This seems to works fine (and work also with the arrow keys, Enter / Return, etc.):
If Key = Chr(9) Then
I use this.
Function CellKeyDown(row as Integer, column as Integer, key as String) As Boolean
select case asc(key)
case 9
if column = me.ColumnCount-1 and row = me.ListCount-1 then return false
if column = me.ColumnCount-1 then
row = row + 1
column = 0
me.EditCell(row, column)
else
me.EditCell(row, column + 1)
end if
me.ActiveCell.UseFocusRing = false
return true
end select
End Function
or additionally with ENTER ,UP, DOWN, LEFT, RIGHT Keys
Function CellKeyDown(row as Integer, column as Integer, key as String) As Boolean
select case asc(key)
case 9, 29, 13
if column = me.ColumnCount-1 and row = me.ListCount-1 then me.EditCell(0,0)
if column = me.ColumnCount-1 then
if column = me.ColumnCount-1 and row = me.ListCount-1 then
me.EditCell(0,0)
else
row = row + 1
column = 0
me.EditCell(row, column)
end if
else
me.EditCell(row, column + 1)
end if
me.ActiveCell.UseFocusRing = false
return true
case 31
if row = me.ListCount-1 then
row = 0
me.EditCell(row, column)
else
me.EditCell(row + 1, column)
end if
me.ActiveCell.UseFocusRing = false
return true
case 30
if row = 0 then
row = me.ListCount-1
me.EditCell(row, column)
else
me.EditCell(row - 1, column)
end if
me.ActiveCell.UseFocusRing = false
return true
case 28
if Column = 0 then
if row = 0 then
me.EditCell (me.ListCount-1,me.ColumnCount-1)
else
row = row -1
column = me.ColumnCount-1
me.EditCell(row, column)
end if
else
me.EditCell(row, column - 1)
end if
me.ActiveCell.UseFocusRing = false
return true
end select
End Function
Axel:
I tried some minutes your second example. It works a bit strange to me, but seems better than my code. I will check it in more depth this afternoon (and add an Edit ON/OFF Mode If
block) and see it if fits better my needs (it seems so right now).
Thank you for this alternate and interesting (and useful of course) way of coding.
select case asc(key)
I would not wrote that, but that allows to not use chr() everytime: good idea.
case 9, 29, 13
I would wrote case 9, 13, 29
, or more precisely, I woul use three Cases
and wrote the arrow keys sequencially (28, 29, 30, 31). Does this make a difference ? To my eyes: yes, to the application ? Certainly no.
Different brains, different way(s) to make things: always something interesting to look at.
axel:
did you try the code you provide ?
Also, I do not found in the documentation:
me.ActiveCell.UseFocusRing
I found ActiveCell
, but it is said to be read only.
I never use the focus ring in the ListBox on OSX
But OSX also paints a focus ring when editing a cell.
‘me.ActiveCell.UseFocusRing = false’ prevents it
While I understand the why of drawing the focus UI, in that particular case, you are 100% right: the usual height of a Row (at Edit time) is too small. And, after all, the user is editing the Cell and knows that.
I too under cicumstances, but usually, I uncheck the UseFocus ring (excepted for Canvas, sometimes, when needed: one Canvas, some other controls).
IMHO.