Search string in EDITBOX

Moderator: Rathinagiri

User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Search string in EDITBOX

Post by serge_girard »

Hello,


I'm a member since october last year and rebuild (from Clipper 5.2.) some very large applications in HMG. It's working very fine!

Now I have a new challenge:

Is it possible to search for a string in a EDITBOX (filled up with memoRead) and if found to auto-select that piece of string in the EDITBOX (To show where in the EDITBOX the searchstring is found) ?

I have a TEXTBOX for the searchstring which activates a function with the AT() function so I can find the position but I don't know how to move the mouse to select>

Any body any idea?


Thx and greetings,

Serge
There's nothing you can do that can't be done...
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Search string in EDITBOX

Post by Rathinagiri »

Hi,

Please consider this small sample. :)

Code: Select all

#include <hmg.ch>

Function Main
   local cText :=  'LAWS RELATING TO WIRING, TEST, AND OPERATION:' + chr( 13 ) + chr( 10 ) + ;
                  '' + chr( 13 ) + chr( 10 ) + ;
                  '1.  Any wire cut to length will be too short.' + chr( 13 ) + chr( 10 ) + ;
                  '2.  Milliampermeters will be connected across the power source, voltmeters in series with it.' + chr( 13 ) + chr( 10 ) + ;
                  '3.  The probability of an error in the schematic is directly proportional to the trouble it can cause.' + chr( 13 ) + chr( 10 ) + ;
                  '4.  Identical units tested under identical conditions will not be identical on the final test after being buried under other components and wiring.' + chr( 13 ) + chr( 10 ) + ;
                  "5.  A self starting oscillator won't." + chr( 13 ) + chr( 10 ) + ;
                  '6.  A crystal oscillator will oscillate at the wrong frequency -- if it oscillates at all.' 
                        
   define window ebs at 0, 0 width 400 height 300 main
      define textbox search
         row 10
         col 10
         width 200
      end textbox
      define button searchbutton
         row 10
         col 220
         width 80
         caption 'Search'
         action doeditboxsearch()
      end button
      define editbox eb
         row 40
         col 10
         width 380
         height 200
         value cText
      end editbox
   end window
   ebs.center
   ebs.activate
Return


function doeditboxsearch
   local cString := ebs.search.value
   local cText := ebs.eb.value
   local nStart := at( cString, cText )
   if nStart > 0
      TextBoxEditSetSel( 'ebs', 'eb', ( nStart - 1 ), ( nStart - 1 ) + len( cString ) )
      ebs.eb.setfocus
   endif
   return nil




function TextBoxEditSetSel( cParent, cControl, nStart, nEnd )
   local nHandle := GetControlHandle( cControl, cParent )
   TextBoxSetSel( nHandle, nStart, nEnd )
   return nil




#pragma BEGINDUMP

#include <windows.h>
#include <commctrl.h>
#include "hbapi.h"
#include <wingdi.h>

HB_FUNC ( TEXTBOXSETSEL )
{
   HWND hWnd1;
   hWnd1 = (HWND) hb_parnl (1);
   SendMessage((HWND) hWnd1,EM_SETSEL, (WPARAM)(int) hb_parni(2),(LPARAM) (int) hb_parni(3));
}

#pragma ENDDUMP
   
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Search string in EDITBOX

Post by serge_girard »

Thank you rathinagiri!

This works fine!

Now I can enhance it to make it Search->Next

Bye & thx,

Serge
There's nothing you can do that can't be done...
User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Search string in EDITBOX

Post by serge_girard »

Good idea!

But this functions needs to be improved:

1) Multiple 'founds'. Only the first string is found.
2) In a long textstring (76Kb) the searchstring is found but you have to scroll down for yourself....

Anybody?


Serge
There's nothing you can do that can't be done...
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Search string in EDITBOX

Post by Rathinagiri »

Serge,

Can you please post a sample?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
PeteWG
Posts: 176
Joined: Sun Mar 21, 2010 5:45 pm

Re: Search string in EDITBOX

Post by PeteWG »

rathinagiri wrote:Hi,
Please consider this small sample. :)
Hi rathinagiri,

Nice sample!

I have made an improvement to support multiple find. in case someone needed it, code is below:

Code: Select all

#include <hmg.ch>

FUNCTION MAIN()
   
   LOCAL cText :=  'LAWS RELATING TO WIRING, TEST, AND OPERATION:' + Chr( 13 ) + Chr( 10 ) + ;
         '' + Chr( 13 ) + Chr( 10 ) + ;
         '1.  Any wire cut to length will be too short.' + Chr( 13 ) + Chr( 10 ) + ;
         '2.  Milliampermeters will be connected across the power source, voltmeters in series with it.' + Chr( 13 ) + Chr( 10 ) + ;
         '3.  The probability of an error in the schematic is directly proportional to the trouble it can cause.' + Chr( 13 ) + Chr( 10 ) + ;
         '4.  Identical units tested under identical conditions will not be identical on the final test after being buried under other components and wiring.' + Chr( 13 ) + Chr( 10 ) + ;
         "5.  A self starting oscillator won't." + Chr( 13 ) + Chr( 10 ) + ;
         '6.  A crystal oscillator will oscillate at the wrong frequency -- if it oscillates at all.'
   
// SET AUTOADJUST ON NOBUTTONS

  cText := hb_MemoRead( "c:\hmg.3.0.46\changelog.txt" )
   
   DEFINE WINDOW ebs At 0, 0 WIDTH 500 HEIGHT 300 MAIN
      DEFINE TEXTBOX search
         Row 10
         Col 10
         WIDTH 200
      END TEXTBOX
      DEFINE BUTTON SearchButton
         Row 10
         Col 220
         WIDTH 80
         CAPTION 'Search'
         ACTION doeditboxsearch( .F. )
      END BUTTON
      DEFINE BUTTON ContinueSearch
         Row 10
         Col 310
         WIDTH 80
         CAPTION 'Find next'
         ACTION doeditboxsearch( .T. )
      END BUTTON
      DEFINE EDITBOX eb
         Row 40
         Col 10
         WIDTH 450
         HEIGHT 200
         VALUE cText
      END EDITBOX
   END WINDOW
   ebs.center
   ebs.activate
   RETURN NIL
   
FUNCTION doeditboxsearch( lFindNext )
   STATIC nStartSearch := 0
   LOCAL cString := ebs.search.value
   LOCAL cText := ebs.eb.value
   LOCAL nStart
   
   hb_Default( @lFindNext, .F. )
   IF ! lFindNext
      nStartSearch := 0
   ENDIF
   
   nStart := hb_AtI( cString, cText, nStartSearch )  // case-insensitive search
   
   IF nStart > 0
      
      ebs.eb.setfocus
      TextBoxEditSetSel( 'ebs', 'eb', ( nStart - 1 ), ( nStart - 1 ) + Len( cString ) )
      nStartSearch := ( nStart  ) + Len( cString ) -1
      
   ENDIF
   RETURN NIL
   
FUNCTION TextBoxEditSetSel( cParent, cControl, nStart, nEnd )
   LOCAL nHandle := GetControlHandle( cControl, cParent )
   TextBoxSetSel( nHandle, nStart, nEnd )
   RETURN NIL
   
#pragma BEGINDUMP

#include <windows.h>
#include <commctrl.h>
#include "hbapi.h"
#include <wingdi.h>

HB_FUNC ( TEXTBOXSETSEL )
{
   HWND hWnd1;
   hWnd1 = (HWND) hb_parnl (1);
   SendMessage((HWND) hWnd1,EM_SETSEL, (WPARAM)(int) hb_parni(2),(LPARAM) (int) hb_parni(3));
}

#pragma ENDDUM
Notes:
---------
1. EDITBOX control does not support auto-scrolling to the new found position. (setting CaretPos doesn't help scrolling.)
2. I have interchanged EDITBOX with RICHEDITBOX control that support auto-scrolling but.. i did notice a strange behaviour:
selected text is shifted some positions next to the found text. Try it yourself to see what i mean.
3. Tested with both hmg 3.0.46 and minigui extended. same results.

I hope someone may have an idea why it happens..

regards,

---
Pete
User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Search string in EDITBOX

Post by serge_girard »

Rathinagiri,

Here is an example.

The first 3 'finds' of 'jaja' are visible, for the others you have to scroll down.
Attachments
search.rar
(1.71 KiB) Downloaded 315 times
There's nothing you can do that can't be done...
User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Search string in EDITBOX

Post by serge_girard »

Rathinagiri,

I enhanced your exemple with Find-Next
There's nothing you can do that can't be done...
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Search string in EDITBOX

Post by Pablo César »

Serge_girard, see implemented to scroll caret editbox:

Code: Select all

#include <hmg.ch>

#define SILVER          {204,204,204}

Function Main
PUBLIC start

cInput_file	:= memoRead("BATCHOUT.TXT")
cEDIT			:=	'JAJA'
Start			:= 1


DEFINE WINDOW SEARCH AT 0, 0 WIDTH 600 HEIGHT 600 MAIN

ON KEY ESCAPE ACTION {SEARCH.Release}

@ 30,5 EDITBOX Edit_1 ;
	WIDTH 550        ;
	HEIGHT 500       ;
	VALUE cInput_file ;
    FONT "Courier New" ;
	MAXLENGTH 255 				

@ 5,10 TEXTBOX T_SEARCH;
	TOOLTIP 'Search'	;
	WIDTH 150 ;
	MAXLENGTH 20	;
	VALUE cEDIT     ;
	BACKCOLOR SILVER	;
	FONTCOLOR BLUE BOLD  ;
	ON CHANGE Start_From_One()

@ 5,170 BUTTON bt_Search  ;
	CAPTION '&Search'             ;
	WIDTH 150 HEIGHT 22    ;
	FONT 'Arial' SIZE 9 FLAT ;
	ACTION { Search_Output(Start) } ;

	DEFINE MAIN MENU
		POPUP 'TEST'						 
			ITEM 'TEST AT() '  			//ACTION TEST_AT() 
		END POPUP
	END MENU
END WINDOW
CENTER WINDOW SEARCH
ACTIVATE WINDOW SEARCH
RETURN

FUNCTION Start_From_One()
START = 1
RETURN

FUNCTION Search_Output(xStart)
Local cEDIT_String	:= SEARCH.T_SEARCH.value
Local cText			:= memoRead("BATCHOUT.TXT")	// SEARCH.Edit_1.value
Local nHandle := GetControlHandle("Edit_1","SEARCH")

cText		  		    := UPPER(ALLTRIM(cText))
cEDIT_String			:= UPPER(ALLTRIM(cEDIT_String))
nLN						:= LEN(cEDIT_String)

IF  nStart := AT(cEDIT_String, cText )  > 0	
	lFND := .f.
	FOR A = 1 TO LEN(cText) - nLN + 1
  	    IF SUBSTR(cText, xStart, xStart + nLN - 1) = cEDIT_String
			TextBoxEditSetSel( 'SEARCH', 'Edit_1', ( xStart - 1 ), ( xStart - 1 ) + LEN( cEDIT_String ) )
			SEARCH.Edit_1.setfocus
			lFND := .t.
			EXIT
		ENDIF
	   xStart++
	NEXT A 
	IF !lFND
		MSGINFO('NO MORE ' + cEDIT_String, 'ok')
		xStart = 0
	ENDIF
ELSE
	MSGINFO(cEDIT_String + ' NOT found', 'ok')
	xStart = 0
ENDIF
ScrollCaret(nHandle)
Start	:= xStart + 1
RETURN

FUNCTION TextBoxEditSetSel( cParent, cControl, nStart, nEnd )
LOCAL nHandle := GetControlHandle( cControl, cParent )
TextBoxSetSel( nHandle, nStart, nEnd )
RETURN nil


#pragma BEGINDUMP

#include <windows.h>
#include <commctrl.h>
#include "hbapi.h"
#include <wingdi.h>

HB_FUNC ( TEXTBOXSETSEL )
{
   HWND hWnd1;
   hWnd1 = (HWND) hb_parnl (1);
   SendMessage((HWND) hWnd1,EM_SETSEL, (WPARAM)(int) hb_parni(2),(LPARAM) (int) hb_parni(3));
}

HB_FUNC ( SCROLLCARET )
{
   HWND hWnd = (HWND) hb_parnl (1);
   SendMessage((HWND) hWnd,EM_SCROLLCARET,(WPARAM)(int) 1,(LPARAM)(int) 0);
}
#pragma ENDDUMP
i think it is what you are looking for... :)
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
serge_girard
Posts: 3166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Search string in EDITBOX

Post by serge_girard »

Pablo ,


This exactly what I was looking for and it all works very fine and fast!

Thx all !!

Serge
There's nothing you can do that can't be done...
Post Reply