Here is some code to demonstrate the  AutoComplete property  Its behavior is very similar to Outlook 2003 or the Internet Explorer Address URL entry control. It prompts the user with possible entries in a drop down like Intellisense.


The code creates a form with 2 textboxes, a grid and a listbox from which you can change the Autocomplete behavior of the other controls. It also shows the Autocomplete table, so as you enter values into the textboxes or the grid, you can see the autocomplete table contents being updated. If you enter a new value, a new record is added. If you use an existing value, the count increments and the datetime stamp is updated for that value.


Tab around the form (Ctrl-Tab to get out of the grid) and type in various values. Hit the Delete key when an Autocomplete entry is highlighted, and it will be removed. (The same behavior can be seen in Outlook 2003.) Experiment with changing the listbox to Most Recently Used or Most Frequently Used.


Of course, because it’s a normal Visual Foxpro table, you can modify the table contents from your own code. For example, it can be prepoplulated with any desired values, perhaps depending on the day of week or other values.


The sorting of the menu is either alphabetical or based on an integer nSortWeight determined by the mode. The Most Frequently Used value is simple: it just sets nSortWeight to the negative value of the Count field. The Custom mode is similar: it sets nSortWeight to the negative value of the Weight field.

The MRU field is a little more complicated: it subtracts the current DateTime from the Updated field’s DateTime value and multiplies by 86400 (the number of seconds in a day) to get an integer. Can you figure out under what conditions this value will overflow? Hint: a 4 byte integer needs to accommodate positive and negative values. The integer part of a DateTime value represents a single day. The fractional part is the number of seconds since midnight divided by 86400. What is the resolution of the MRU mode for a fast typist?



SET REFRESH TO 2  &&Refresh the BROWSE every 2 seconds


CREATE Table AutoComp (Source c(20), Data C(254), Count I, Weight I, Created T, Updated T, User M)

INDEX ON source +UPPER(LEFT(data,30))+padl(count,8) FOR !DELETED() tag data



USE (HOME()+"samples\data\customer")      && use table to get sample data


      INSERT into autocomp values ("TXTNAME",,1,0,DATETIME()-1*86400*RECNO(),DATETIME()-2*86400*RECNO(),"")

      INSERT into autocomp values ("TXTCITY",,1,0,DATETIME()-3*86400*RECNO(),DATETIME()-4*86400*RECNO(),"")



PUBLIC ox,oBrowse


USE autocomp order 1

BROWSE NOWAIT NAME oBrowse    && show the autocomp table




CREATE CURSOR temp (name c(30),city c(30))      && temp table for grid

APPEND blank      && add 3 records

APPEND blank

APPEND blank

GO TOP            && go to the first record

PUBLIC oAutoComp as form


WITH oAutoComp as Form



      .AddObject("lblName" ,"label")

      WITH .lblName






      WITH .txtName as TextBox





      .AddObject("lblCity" ,"label")

      WITH .lblCity






      WITH .txtCity as textbox





      WITH .grid1 as Grid





      WITH .grid1.column1





      WITH .grid1.column2






      WITH .lst








WITH oBrowse as Grid    && position the browse








DEFINE CLASS MyListBox as ListBox

      PROCEDURE init

            WITH this as ListBox



                  .AddItem("Most Frequently")

                  .AddItem("Most Recently")



            this.ListIndex=2  && initialize to Alphabetical


      PROCEDURE click

            * change all controls that have Autocomplete property