incremental search

The place for peer-to-peer support for Visual ProMatrix versions older than VPME 9.1.

incremental search

Postby LGL » Sun Sep 28, 2014 6:37 pm

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

**************************************************


--------------------------------------------------------------------------------
LGL
 
Posts: 2
Joined: Mon Sep 01, 2014 4:58 am

Re: incremental search

Postby Bob » Mon Sep 29, 2014 7:35 am

The VPM framework supports incremental searches in the default tool set, ie toolbar list, F2 lookup, in all columns if set up to do so. If you can describe your needs more fully, it might be there already.
Bob
 
Posts: 718
Joined: Mon Jun 14, 2010 6:53 am
Location: Near Albany, NY

Re: incremental search

Postby cti2 » Mon Sep 29, 2014 7:52 am

All the code that you found on Google makes me dizzy.
Too complicated for my feeble 64 year old cranium. I need things simple.
I use the following code in a text box control on an Invoice form to lookup customers by there code and then fill in all the address info on the invoice. This is the only code needed to do this. I use similar code throughout my apps.
In the InteractiveChange Method........
Linspoint=This.SelStart
lcSearchStr=SUBS(This.Value,1,Linspoint)
IF !EMPT(lcSearchStr)
=SEEK(UPPE(lcSearchStr),[custom],[custcode])
IF FOUND([custom])
This.Value = ALLT(custom.custcode)
=ThisForm._LoadNewCust() && onform method that replaces other invoice fields with the customer values.
=INSMODE(.F.)
ELSE
This.Value = lcSearchStr
=ThisForm._zClearCustdata() && onform method to clear all associated invoice fields.
=INSM(.T.)
ENDIF
ELSE
This.Value=SPAC(0)
=ThisForm._zClearCustdata()
ENDIF
This.Parent.Refresh()
This.SelStart = Linspoint
HTH, Pete Ryder
cti2
 
Posts: 181
Joined: Wed Oct 13, 2010 6:38 am


Return to VPM - older versions

Who is online

Users browsing this forum: No registered users and 2 guests

cron