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)
USE (HOME()+”samples\data\customer”) && use table to get sample data
SCAN NEXT 10
INSERT into autocomp values (“TXTNAME”,Customer.contact,1,0,DATETIME()-1*86400*RECNO(),DATETIME()-2*86400*RECNO(),””)
INSERT into autocomp values (“TXTCITY”,Customer.city,1,0,DATETIME()-3*86400*RECNO(),DATETIME()-4*86400*RECNO(),””)
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
GO TOP && go to the first record
PUBLIC oAutoComp as form
WITH oAutoComp as Form
WITH .txtName as TextBox
WITH .txtCity as textbox
WITH .grid1 as Grid
WITH oBrowse as Grid && position the browse
DEFINE CLASS MyListBox as ListBox
WITH this as ListBox
this.ListIndex=2 && initialize to Alphabetical
* change all controls that have Autocomplete property