Hello,
I am trying to do an incremental search from a textbox. I googled this topic and found this below
by marcia akins on msdn. also a newbie to VFP
can anybody show me where to place it in my form and how to activate it? or use it?
thanks a million
lgl
below is the code
**************************************************
*-- Class: txtsearch
*-- ParentClass: textbox
*-- search textbox: synchronizes all other form controls with search results
*
DEFINE CLASS txtsearch AS textbox
Height = 23
Width = 209
*-- Alias to search in. Required if the text box is not bound
calias = ""
*-- Tag to use in search
ctag = ""
*-- Name of the field in the alias to use in the SEEK or LOCATE into that alias
cfield = ""
*-- When set to true, refreshes the parent container (usually a page or a form) as the value is typed into the text box
lrefreshparent = .T.
cExact = ""
Name = "txtsearch"
PROCEDURE gotfocus
Textbox::GotFocus()
This.SelStart = 1
This.SelLength = LEN( This.Text )
NODEFAULT
This.cExact = SET( "EXACT" )
SET EXACT OFF
ENDPROC
PROCEDURE LostFocus
LOCAL lcExact
lcExact = This.cExact
SET EXACT &lcExact
ENDPROC
*-- As the name implies...
PROCEDURE handlekey
LOCAL lcSofar, lnSelect, lnSelStart, lnSelLength
WITH This
*** Get the value typed in so far
lnSelStart = IIF( LASTKEY() # 127, .SelStart, .SelStart - 1 )
*** Handle and empty value in the text box
IF lnSelStart = 0
.Value = ''
.SelStart = 0
GO BOTTOM IN ( .cAlias )
SKIP IN ( .cAlias )
ELSE
lcSofar = UPPER( LEFT( TRANSFORM( .Value ), lnSelStart ) )
.Value = lcSoFar
*** Use seek to find the record if a tag was provided
IF ! EMPTY( .cTag )
IF SEEK( Str2Exp( lcSoFar, TYPE( .cAlias + [.] + .cField ) ), .cAlias, .cTag )
***********************************************************************
*** Changed By.: Marcia G. Akins on 10 May 2005
*** Reason.....: Make sure the value in the textbox is always a character string
***********************************************************************
.Value = TRANSFORM( EVAL( .cAlias + '.' + .cField ) )
***********************************************************************
ENDIF
ELSE
*** Otherwise, save the current work area before swithching to the specified table
lnSelect = SELECT()
SELECT ( .cAlias )
*** And locate the specified record
IF TYPE( .cField ) $ [C,M]
LOCATE FOR UPPER( ALLTRIM( EVAL (.cField ) ) ) = lcSoFar
ELSE
***********************************************************************
*** Changed By.: Marcia G. Akins on 10 May 2005
*** Reason.....: Make numeric fields behave more like character fields
***********************************************************************
LOCATE FOR TRANSFORM( EVALUATE( .cField ) ) = lcSoFar
***********************************************************************
ENDIF
IF FOUND()
***********************************************************************
*** Changed By.: Marcia G. Akins on 10 May 2005
*** Reason.....: Make sure the value in the textbox is always a character string
***********************************************************************
.Value = TRANSFORM( EVAL( .cAlias + '.' + .cField ) )
***********************************************************************
ENDIF
SELECT ( lnSelect )
ENDIF
ENDIF
*** If we need to refresh the parent container (usually a form or a page, do it here
IF .lRefreshParent
.RefreshParent()
ENDIF
*** Highlight the portion of the value after the insertion point
.SelStart = lnSelStart
lnSelLength = LEN( TRANSFORM( .Value ) ) - lnSelStart
IF lnSelLength > 0
.SelLength = lnSelLength
ENDIF
*** If we have refreshed the controls in the parent container, there are timing issues to overcome
*** Even though .SelStart and .SelLength have the correct values at this point, the search box
*** does not appear highlighted correctly without this delay
DOEVENTS
ENDWITH
ENDPROC
*-- Synchronise the text boxe's value with the rest of the data on the form, for example, when the user navigates to a new record using the toolbar or other means
PROCEDURE synchronize
LOCAL lnSelStart, lnSelLen
*** Synchronize the text box's value with the current record on the screen
WITH This
lnSelStart = .SelStart
.Value = TRANSFORM( EVAL( .cAlias + '.' + .cField ) )
.SelStart = lnSelStart
lnSelLen = LEN( ALLTRIM( TRANSFORM( .Value ) ) ) - lnSelStart
.SelLength = IIF( lnSelLen > 0, lnSelLen, 0 )
DoEvents
ENDWITH
ENDPROC
*-- Refresh all the controls in the parent without refreshing this one
PROCEDURE refreshparent
LOCAL loControl
FOR EACH loControl IN This.Parent.Controls
IF loControl.name # This.name
*** Make sure the control has a refresh method!!!!
IF PEMSTATUS( loControl, 'Refresh', 5 )
loControl.Refresh()
ENDIF
ENDIF
ENDFOR
ENDPROC
PROCEDURE Refresh
*** Synchronize the display when another means is used to navigate to a new record
This.Synchronize()
dodefault()
ENDPROC
PROCEDURE InteractiveChange
*** If the key pressed was a printable character or a backspace, handle the keystoke and search
IF This.SelStart > 0
IF ( LASTKEY() > 31 AND LASTKEY() < 128 ) OR ( LASTKEY() = 7 )
This.HandleKey()
ENDIF
ENDIF
ENDPROC
*-- Special stuff needed to initialize the text box
PROCEDURE setup
ENDPROC
ENDDEFINE
*
*-- EndDefine: txtsearch
**************************************************
--------------------------------------------------------------------------------