Navigation with a character in a ListBox

I have implemented a “navigation in the ListBox” using the a-z letter pressed in the keyboard.

While a and z keypresses works fine (easy to do), I had hard times (in fact, I failed) to implement the other letters (convert base 26 into base 100):

Press the letter b displays line 2 if the ListBox have 26 lines…

My computations are always false and I cannot finish this feature.

A little help is welcome.

b = line 2

h= line 8?

If so,

dim thekey as string
dim therow as integer
thekey = uppercase(letterpressed)  //now we have A..Z
if thekey >="A" and thekey <="Z"  then
therow = ascb(thekey) - 65
if mylistbox.listcount > therow then
mylistbox.listindex = therow
end if
end if

Apologies if I have over simplified.

If you want to jump to the first item which starts with that letter, you need a different answer

Hi Jeff,

Thank you for your answser.

I do not took - yet - my early morning cup of coffee, so I do not know if this is what I need. But, at first, the question was not “How to jump to the first entry that start with the pressed key” ;-:slight_smile: I think I saw an answer for that month ago.

Also, as you certainly understand, I do not want to go to specific line(s) ;-:slight_smile: , but to relative lines: pressing the h key will display different lines depending on the total number of lines the Row holds.

I found that idea nice when it come to mind, and had hard time to code it for … unknow reason (too old to make rock and roll ?)

Thank you for taking time to write this answer. Have a nice week-end.

Jeff, all,

I do not know if you code unlock my brain on the subject, but I found the solution:

There are 26 characters in our alphabet (a-z)
I want to convert that to 100%
So each character from a to z is 1/26° of 100 (for some reason this talked to me and was the starting point to the code below.

So the formula is:

(LB.ListCount \\ 26) * TheKeyNbr

LB.ListCount Number of Rows in the ListBox

26 Characters in the alphabet,

TheKeyNumber Number of the typed ASC Character - 64 (so ASC(A) - 64 = 1)

Working code:

[code] Dim TheKey As String
Dim TheKeyNbr As Integer

// Convert the Key to UpperCase (Typing A-Z or a-z will be converted to A-Z)
TheKey = UpperCase(Key)

// Convert the typd Key to Integer
TheKeyNbr = Asc(TheKey) - 64 // If the Key is A, A = 65, TheKeyNbr value will be 1… and so on

// Try to move inside the List
If TheKeyNbr > 0 And TheKeyNbr < 27 Then
// Compute and set the Row to display
LB.ListIndex = (LB.ListCount \ 26) * TheKeyNbr

// Exit this Event
Return True

End If[/code]

The explanation may not be 100% accurate (or good english), but the code seems to works fine !

I put it into ListBox.KeyDown.

Enjoy !

You code is just fine, but I would rather use it for 1234567890 as a mean to jump to the n tenth of the rows.

Pressing an alphabetic key to get to n 26th portion of a listbox is not very intuitive. Usually, when an alphabetic key is pressed, the user will expect accessing the first row with the same initial.

You better compare to clicking (or fn-top / bottom arrow) in the ScrolllBar than thinking alphabetical.

Also, sometimes the first row (and why the first row ?) does not contains text, but an index (either a magazine number or anything else—like a matrix number).

So, for these cases, using a letter as a navigation purpose is as good as any other method.

BUT: you are right in your answer. Sometimes people may forgot the obvious. I could have used numbers (0-9… 1-0), but with a non US keyboard, one have to press the shift key to get the digits… dig it… dig it (The Beatles).

Have a nice week-end.

PS: just before sending this text, I re-re-read your answer 'cause I do not understand what you meant in the first sentence ! Yes, now I understand… Niiiice ! But as I sai earlier, that would mean shift-1234567890 (pick one). I do not think at that, at first, nor at second.

In the other way, don’t we talk about the alpha and the omega ?
If users complains, I may change that (and use an easier code…)
I really had that in mind since the beginning.

At least for the French keyboard, you can also use dictionary or replaces to have & = 1, é = 2 and so on. In practice whether or not the user presses shift, the app reacts the same.

[quote=223555:@Emile Schwarz]In the other way, don’t we talk about the alpha and the omega ?
If users complains, I may change that (and use an easier code…)

You know we try to build idiot proof, but the universe keeps making better idiots, and usually wins. No later than yesterday I got a customer who demanded a refund because when he bought in the middle of the night he could not get someone on the phone to explain to him how to install a font when the Read Me file explains that at length, plus an email sent upon purchase. He got furious because he wanted a setup program. Such imbeciles will post nasty reviews before you have a chance to improve your program.

Yes Michel, you are right… good idea (I do not like it because of the explanation to the end-user… read your own text below) *

Yes, indeed…

  • Apple’s Preview keyboard shortcuts is an excellent BAD example in this domain: it shows exactly what you said, but who knows where the Ç key is in a French keyboard ? (It’s the 9 key)
    to activate that function (scale to window size), one have to press cmd-shift-9,
    to activate Real Size, the same one have to press cmd-0 (zero).
    Perturbing, isn’t it ?

What must I press in the Keyboard for the cmd-[ or cmd-] shortcuts ?
Of course: French keyboard mapping and French on screen.

So, with examples like these, I am a little reluctant to use some combinations…

I just recall this…

Don’t you remember users never read manuals ? (and this is an old one !)
RTFM was created for that.

Now, the problem is gone: there is no more manual(s) with computers…