Watch the Autocomplete values change as you enter data


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?


 


CLEAR ALL


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


_screen.AutoCompTable=”autocomp.dbf”


 


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(),””)


ENDSCAN


 


PUBLIC ox,oBrowse


SELECT 2


USE autocomp order 1


BROWSE NOWAIT NAME oBrowse    && show the autocomp table


 


SELECT 1


 


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


oAutoComp=NEWOBJECT(“form”)


WITH oAutoComp as Form


      .AllowOutput=.f.


      .Height=400


      .AddObject(“lblName” ,”label”)


      WITH .lblName


            .Top=10


            .Left=40


            .Caption=”Name”


      ENDWITH


      .addobject(“txtname”,”textbox”)


      WITH .txtName as TextBox


            .top=10


            .left=90


            .width=200


      ENDWITH


      .AddObject(“lblCity” ,”label”)


      WITH .lblCity


            .Top=40


            .Left=40


            .Caption=”City”


      ENDWITH


      .AddObject(“txtcity”,”textbox”)


      WITH .txtCity as textbox


            .top=40


            .left=90


      ENDWITH


      .AddObject(“grid1″,”grid”)


      WITH .grid1 as Grid


            .top=80


            .left=30


            .height=100


      ENDWITH


      WITH .grid1.column1


            .AddObject(“txtName”,”textbox”)


            .currentcontrol=”txtName”


            .width=130


      ENDWITH


      WITH .grid1.column2


            .AddObject(“txtCity”,”textbox”)


            .currentcontrol=”txtCity”


            .width=130


      ENDWITH


      .AddObject(“lst”,”mylistbox”)


      WITH .lst


            .top=210


            .left=20


      ENDWITH


      .SetAll(“visible”,.t.)


      .Visible=.t.


ENDWITH


 


WITH oBrowse as Grid    && position the browse


      .left=oAutoComp.Left+oAutoComp.Width


      .width=650


      .Height=oAutoComp.Height*2


      .FontSize=10


      .AutoFit


ENDWITH


 


DEFINE CLASS MyListBox as ListBox


      PROCEDURE init


            WITH this as ListBox


                  .AddItem(“None”)


                  .AddItem(“Alphabetical”)


                  .AddItem(“Most Frequently”)


                  .AddItem(“Most Recently”)


                  .AddItem(“Custom”)


            ENDWITH


            this.ListIndex=2  && initialize to Alphabetical


            this.click       


      PROCEDURE click


            * change all controls that have Autocomplete property


            thisform.setall(“autocomplete”,this.ListIndex1)     


ENDDEFINE


 


 


83977

Comments (7)

  1. Calvin,

    I love VFP 9’s autocomplete implementation except for how the dropdown list is rendered. Do you have any ideas on how one might use VFP 9’s new Win32 bindevent() capabilities to position and size the autocomplete dropdown so that it left aligns to its parent control and uses a width identical to its parent?

    Thank you,

    Malcolm

  2. Asif says:

    This code not work because there is no use of autocomplete commands

  3. Aaron says:

    Is there a problem with autocomplete when the textbox is in a toolbar?  I’ve altered your code to show you that it only accepts one character…

    CLEAR ALL

    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

    _screen.AutoCompTable="autocomp.dbf"

    USE (HOME()+"samplesdatacustomer")      && 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(),"")

    ENDSCAN

    PUBLIC ox,oBrowse

    SELECT 2

    USE autocomp order 1

    BROWSE NOWAIT NAME oBrowse    && show the autocomp table

    SELECT 1

    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 oTool as Toolbar

    m.oTool = CREATEOBJECT(‘toolbar’)

    m.oTool.Show()

    m.oTool.AddObject(‘txtName’,’Textbox’)

    m.oTool.txtName.visible = .T.

    m.oTool.txtName.AutoCompTable = _SCREEN.AutoCompTable

    m.oTool.txtName.AutoComplete = 1

    PUBLIC oAutoComp as form

    oAutoComp=NEWOBJECT("form")

    WITH oAutoComp as Form

         .AllowOutput=.f.

         .Height=400

         .AddObject("lblName" ,"label")

         WITH .lblName

               .Top=10

               .Left=40

               .Caption="Name"

         ENDWITH

         .addobject("txtname","textbox")

         WITH .txtName as TextBox

               .top=10

               .left=90

               .width=200

         ENDWITH

         .AddObject("lblCity" ,"label")

         WITH .lblCity

               .Top=40

               .Left=40

               .Caption="City"

         ENDWITH

         .AddObject("txtcity","textbox")

         WITH .txtCity as textbox

               .top=40

               .left=90

         ENDWITH

         .AddObject("grid1","grid")

         WITH .grid1 as Grid

               .top=80

               .left=30

               .height=100

         ENDWITH

         WITH .grid1.column1

               .AddObject("txtName","textbox")

               .currentcontrol="txtName"

               .width=130

         ENDWITH

         WITH .grid1.column2

               .AddObject("txtCity","textbox")

               .currentcontrol="txtCity"

               .width=130

         ENDWITH

         .AddObject("lst","mylistbox")

         WITH .lst

               .top=210

               .left=20

         ENDWITH

         .SetAll("visible",.t.)

         .Visible=.t.

    ENDWITH

    WITH oBrowse as Grid    && position the browse

         .left=oAutoComp.Left+oAutoComp.Width

         .width=650

         .Height=oAutoComp.Height*2

         .FontSize=10

         .AutoFit

    ENDWITH

    DEFINE CLASS MyListBox as ListBox

         PROCEDURE init

               WITH this as ListBox

                     .AddItem("None")

                     .AddItem("Alphabetical")

                     .AddItem("Most Frequently")

                     .AddItem("Most Recently")

                     .AddItem("Custom")

               ENDWITH

               this.ListIndex=2  && initialize to Alphabetical

               this.click      

         PROCEDURE click

               * change all controls that have Autocomplete property

               thisform.setall("autocomplete",this.ListIndex-1)    

    ENDDEFINE