Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Highlighting a row in a Grid typing a letter

Highlighting a row in a Grid typing a letter


This is a Form, with a Grid inside it that performs the selection of an item by two ways:
- double clicking on the desired item
- moving the up and down arrow keys through the items and pressing the enter key

(items are presented in alphabetical order)

The Grid has hundreds of rows (items). When I wish to select an item which begins with 'M' letter (for example, I am searching for "Mango") I navigate more fast using the up and down navigating arrows in the Form vertical bar.

Is it possible to type the M letter and the Grid just highlights on the first item ocurrence beginning with the letter M (without selecting it, because before the "Mango" item I have "Macarroni")?

Form1, KeyPress event:


LPARAMETERS nKeyCode, nShiftAltCtrl

IF nKeyCode=13 AND nShiftAltCtrl=0 

Thank you,

RE: Highlighting a row in a Grid typing a letter

You can trap the alphabetical key in your Keypress event, just as you are doing with the Enter key. You then locate the record in the underlying cursor that you want to highlight. Finally, set focus to the grid.

Something like this:

CODE -->

LPARAMETERS nKeyCode, nShiftAltCtrl

  SELECT TheTable
  LOCATE FOR UPPER(LEFT, TheField, 1)) = UPPER(CHR(nKeyCode))

I posted this in a hurry, so can't guarantee it is 100% correct, but it should give you the general idea.


Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Highlighting a row in a Grid typing a letter

Well, in a Listobx you could simply set IncrementalSearch= .t.

Make it simple, for example this way


lcLastKey = UPPER(LASTKEY())
IF ISAPLPHA(lcLastkey)
   SELECT itensest
   LOCATE FOR produto = lcLastKey

Notice: If FOUND() becomes .F. after LOCATE, you know there is no record starting with the entered letter. Either you then return back to the RECNO you started from or the grid will show the end of the list. It won't matter much, as the next keypress can fix that. Now if you'd like incremental search, eg also be able to search AM to get to the first product starting with AM instead of M, then rather don't do such things with Keypress, have a textbox above the grid header and let the user enter the "begins with" letters there.

Bye, Olaf.

RE: Highlighting a row in a Grid typing a letter

Of course, all this assumes that the grid is read-only. If it wasn't, the keystroke would end up editing the data. Judging from your screen shot, that looks like it's not a problem.


Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Highlighting a row in a Grid typing a letter

Rather than building it yourself, you might check out the LocatorGrid in FoxyClasses: I've just implemented it in a client application and it's fairly easy to use.


RE: Highlighting a row in a Grid typing a letter

Hello colleagues!

I implemented Mike's code in my application and it is working fine.

Olaf, I do not have a Listbox (for letter input) in the Form, only a Grid.

Tamar, I downloaded the Foxy classes, as you advised, for future use. I have to stay in the basics of OOP in VFP for now.

Thank you all.

Thank you,

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close