ListBox: Go to the first entry that starts by…

Sometimes ago, I asked (here) how to navigate alphabetically inside a ListBox.

the answer(s) I got doess not satisfield / satisfy me. So, I let the feature as is…

Hours a go (and a nap away from now), the idea of the code below comes to me and I checked it (with the first text file I had handly, thus the check on Column(1) instead of Column(0).

Paste the code below in the ListBox.KeyDown Event:

[code]Function KeyDown(Key As String) As Boolean
//
// Search the first word that start by Character Key…
//
Dim RowCnt As Integer
Dim LoopIdx As Integer
Dim AZUpper As String
Dim AZLower As String
Dim Number09 As String

// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// Navigation thru the List
// a/A gos to the first word starting with an a/A
// b/B to y/Y
// z/Z gos to the first word starting with a z/Z
//

// Fill some variables
AZUpper = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
AZLower = “abcdefghijklmnopqrstuvwxyz”
Number09 = “0123456789”

// Exclude non ASCII characters
If InStr(AZUpper,Key) = 0 And InStr(AZLower,Key) =0 And InStr(Number09,Key) = 0 Then Exit

// How Many Rows in the ListBox ?
RowCnt = LB.ListCount - 1

// Scan the whole ListBox Cotnents [Column(1)]
For LoopIdx = 0 To RowCnt
// For the testing file, I check Column(1) text (Column(0) holds "Frew iiii"…)

// Is this Column start by a “Key” ?
If Left(LB.Cell(LoopIdx,1),1) = Key Then
  LB.ListIndex = LoopIdx
  
  // Exit this Handler
  Return True // Exit was not a solution
End If

// To avoid 1, Infinite Loop…
If UserCancelled Then Exit

Next

// Select the first entry (to say “not found”)
LB.ListIndex = 0 // Or: LB.ListIndex = RowCnt // Last entry
End Function[/code]

You have a Multi-Columns ListBox and it is filled with data.
Press a key (a-z) and watch what happens.

Nota: in my test file, I had no entry for x, y and z. The selected Row does not change in that case. This can be changed by adding a line after the Next line and set LB.ListIndex = 0 OR LB.ListIndex = RowCnt.

Also, if you want to be able to type more than one letter for the start of teh word to get, you have to add some more code.

Enjoy !

Sorry for the typo(s ?), I cannot remove it.

I just checkd the use of, say, cmd-l and the code above have to be modified to not glob it.