Page 1 of 1

Incremental search routine using Tsbrowse needed

Posted: Mon Oct 08, 2007 5:43 am
by RAMESHBABU
Hi friends

Can anybody share the Incremental Search routine using TsBrowse.

I have a get and as i keep entering values in the get, the same values
should be searched in the attached Tsbrowser.

Thanks

- Ramesh Babu P

Posted: Mon Oct 08, 2007 7:15 am
by Antonio Linares
Ramesh,

Here you have it. In this example a GET is not needed, but you may easily add it:

Code: Select all

#include "FiveWin.ch"

REQUEST DBFCDX

//----------------------------------------------------------------------------//

function Main()

   local oWnd, oBrw, hBmp := ReadBitmap( CurDir() + "\go.bmp" )
   local oSay, cSearch := ""

   USE ( CurDir() + "\Customer" ) VIA "DBFCDX"
   if ! File( CurDir() + "\LAST.CDX" )
      INDEX ON Customer->Last TO ( CurDir() + "\LAST" )
   endif   
   Customer->( OrdSetFocus( "LAST" ) )
   Customer->( DbGoTop() )

   DEFINE WINDOW oWnd TITLE "IncSearch"
   
   @ 1, 1 LISTBOX oBrw ;
      FIELDS hBmp, Customer->Last, Customer->First ;
      HEADERS "", "Last", "First" ;
      SIZE 220, 167
   
   oBrw:bKeyChar = { | nKey, nFlags | Search( nKey, @cSearch ), oBrw:Refresh(),;
                                      oSay:Refresh() } 
   
   @ 14,  2 SAY "Searching:" SIZE 60, 30
   @ 14, 12 SAY oSay PROMPT cSearch SIZE 80, 30
   
   ACTIVATE WINDOW oWnd ;
      ON CLICK MsgInfo( "Click!" )
   
return nil

//----------------------------------------------------------------------------//

function Search( nKey, cSearch )

   if nKey = 8 
      cSearch = SubStr( cSearch, 1, Len( cSearch ) - 1 ) 
   else 
      cSearch += Upper( Chr( nKey ) ) 
   endif
    
   Customer->( DbSeek( cSearch, .t. ) ) 

return nil

//----------------------------------------------------------------------------//

Posted: Mon Oct 08, 2007 10:59 am
by Richard Chidiak
Antonio

Can you help showing a sample with get and incremental search on a listbox ?

Thanks for your time,

Richard

Posted: Mon Oct 08, 2007 12:04 pm
by Antonio Linares
Richard,

Do you mean a browse ?

Same example but typing on a GET ?

Posted: Mon Oct 08, 2007 12:36 pm
by James Bott
Here is a more sophisticated example. It will only accept keystrokes that find a new match in the browse, otherwise it rejects the keystroke. There is a sound for each valid keystroke and no sound for invalid keystrokes.

James

-----------------------
Notes: If you return a logical from the bChange codeblock the GET will either accept or reject the last character. Very useful. Also note the use of a local copy of an instance var for use in a codeblock. I can't figure out why the GET won't accept commas.

Code: Select all

  // Incremental search of the contact list using a GET.
  // Here we process keys dynamically in the contact lookup Get.
  // We have to compensate for the use of the numeric keypad.
   cClient:= space(20)

   // Create a local copy so we can use it in the codeblock below
   // Not needed if you are not using an instance var.
   oLbx2:= ::oLbx2

   // Bug: The GET below doesn't accept commas for some reason.

   @ 25,5 get oGet var cClient of oBar2 ;
     pixel;
     size 120,20 ;
     on change ( nOldRec:=self:cargo:recno(),;
      oGet:nLastKey:=if( getKeyToggle(VK_NUMLOCK), oGet:nLastKey - 48, oGet:nLastKey ),;
      cText:=trim(self:cText)+if(self:nLastKey>=32,chr(self:nLastKey),""),;
      if(self:cargo:seek(upper(cText)), (click(),lAccept:=.t.), (self:cargo:goto(nOldRec),lAccept:=.f. ) ),;
      oLbx2:refresh(),lAccept )

   oGet:cargo:= ::oContact2

function click()
   sndPlaySound("click.wav",2)
return nil

Posted: Mon Oct 08, 2007 2:25 pm
by Richard Chidiak
Antonio ,

Yes

This will be very handy, it has been done with btnget but rather complex to use. I prefer a more flexible solution

Thanks

Richard

Posted: Mon Oct 08, 2007 2:29 pm
by RAMESHBABU
Mr.Antonio, Mr.James

Thank you very much for your samples.

Mr.Antonio's example is very interesting and very simple!.

I could't immediately understood the sample of
Mr.James.

Anyway, I could implement my requirement
very well using Tsbrowse for incremental search in
an array of database fields.

Regards

Ramesh Babu P

Posted: Mon Oct 08, 2007 3:30 pm
by RAMESHBABU
Mr.Richard

I hope this is what you wanted in respect of incremental search with
a get object.

Regards

- Ramesh Babu P

Code: Select all

#include "FiveWin.ch" 

REQUEST DBFCDX 

* Incremental Search with a get object Example

//-----------------------------------------------------------------------// 

FUNCTION Main() 

LOCAL oWnd, oBrw 
LOCAL cSearch, oSearch

USE Customer VIA "DBFCDX" 
INDEX ON Customer->Last TO LAST  
OrdSetFocus( "LAST" ) 
DbGoTop() 

cSearch := SPACE(LEN(Customer->Last)) 

DEFINE WINDOW oWnd TITLE "IncSearch"                ;
       FROM 1,100 TO 212,408 PIXEL NOMAXIMIZE
   
@  4,3 GET oSearch VAR cSearch OF oWnd              ; 
       PIXEL size 145,17                            ;
       PICTURE "@!"                                 

@ 30,1 LISTBOX oBrw                                 ; 
       FIELDS Customer->Last, Customer->First       ; 
       HEADERS "Last","First"                       ; 
       SIZE 300, 155 PIXEL 

* This is the main trick.
oSearch:bPostKey = { || Search(oSearch:oGet:buffer),;
                        oBrw:Refresh() } 
    
ACTIVATE WINDOW oWnd ON INIT WndCenter(oWnd:hWnd)
    
RETURN nil 

//-----------------------------------------------------------------------// 

FUNCTION Search(cSearch ) 

DbSeek( cSearch, .t. ) 

RETURN nil 

**********