[futurebasic] Re: Search Edit Field

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2001 : Group Archive : Group : All Groups

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Sat, 24 Nov 2001 17:33:18 -0500
Continuing his questions, -bowerbird added:

>so a "find" operation should be super-simple using a container, right?
>(i assume it's easy to dump the text of an edit field into a container.)
>
>even a case-insensitive search would just need to use a mid$() command
>to change all lower-case letters to their upper-case perversions, right?

Exactly.

The one thing to consider when dumping the contents of an Edit Field
into a Container for massaging, is that a styled FB Edit Field begins
with a block that defines its length and ends with style information.
In my earlier posted example in this thread, any formatting in a
styled Edit Field will be lost in the search/replace operation.

However, this is not insurmountable. In this commented and simple
demo, we dump the Edit Field text into a container, find the position
of a word in the container, and then transfer that information back
to the Edit Field without touching the contents or styling of the
Edit Field.

In the following example, we will also replace the styled and
selected word "-bowerbird", with "Leslie", while retaining
formatting, by using:

EDIT$(1,-1) = "Leslie"

(I have put a 2 second delay in here so you can see the effect after
pressing the "Find word" button.)

There are other ways to do this with handles, but I have found that
Containers are simple to use, understand and free us of many of the
hassles that come with handles. Their major disadvantage is that they
must be global.

Ken

p.s. There is no logic in this code for multiple searches. We're
simply exploring concepts here.

p.p.s. Please watch for e-mail line breaks and lost underscores.

'---------- END CODE ------------

BEGIN GLOBALS
DIM gC AS CONTAINER
END GLOBALS

LOCAL FN buildWindow
DIM AS RECT w1, ef1, ef2, ef3, b1, b2, b3
DIM AS STR31 s1, s2
EDIT = 2
SETRECT(  w1,   0,   0, 400, 220 )
SETRECT( ef1,  20,  20, 363, 100 )
SETRECT(  b1, 364,  18, 380, 102 )
SETRECT( ef2,  20, 120, 380, 134 )
SETRECT( ef3,  20, 140, 380, 160 )
SETRECT(  b2, 330, 180, 380, 200 )
SETRECT(  b3  180, 180, 315, 200 )

s1 = "Find this string:"
s2 = "-bowerbird"

WINDOW-1,"",            @w1,_docNoGrow
TEXT applFont, 12
EDIT FIELD -1,"",       @ef1,_framed,_leftJust
SCROLL BUTTON -1, 1, 1, 1, 1, @b1, _scrollOther
EDIT FIELD -2, s1,      @ef2,_statNoframed,_leftJust
EDIT FIELD -3, s2,      @ef3,_framed,_leftJust
BUTTON 2,1,"Quit",      @b2,_shadow
BUTTON 3,1,"Find word", @b3,_push
WINDOW 1

EDIT FIELD 0

END FN

CLEAR LOCAL FN doSearch
DIM AS STR255 searchString
DIM AS INTEGER foundPosition, stringLength

// Fill container with contents of Edit Field 1
gC = EDIT$(1)

// Search string equals contents of Edit Field 3
searchString = EDIT$(3)

// Obtain the length of the search string
stringLength = searchString[0]

// Find position of search string in the container
// beginning at first character in container
foundPosition = INSTR(1, gC, searchString)

// Activate Edit Field 1 for SETSELECT
EDIT FIELD 1

// Select the search string in the Edit Field
// Remember, each styled FB Edit Field begins with a block
// that holds the length of the field, so we subtract 1
// from the found position in the container to compensate.
SETSELECT foundPosition - 1, foundPosition + stringLength - 1

// Here is a very simple Replace of the selected word
// using the new EDIT(efNum, -1) replace statement:
DELAY 2000
EDIT$(1,-1) = "Leslie"

// Empty container to save memory and for reuse next time
gC = ""

END FN

LOCAL FN doDialog
DIM AS INTEGER evnt,id
evnt = DIALOG(0)
id = DIALOG(evnt)
SELECT CASE( evnt )
CASE _wndClose
SELECT( id )
CASE 1 : END
END SELECT
CASE _btnClick
SELECT( id )
CASE 2 : END
CASE 3 : FN doSearch
END SELECT
END SELECT
END FN

ON DIALOG FN doDialog

FN buildWindow

// Here is some text for Edit Field 1

DIM AS STR255 t1Str, t2Str

t1Str = "This is some some sample text for ¬
Edit Field 1 that you can try a search and replace ¬
operation on. -bowerbird, this demo uses FN REPLACE ¬
which is designed to work with a container but is ¬
here used to search an edit field."

t2Str = "I am trying to put some extra text into this ¬
edit field simply to activate the scroll button and ¬
be sure that it is activated and working as expected. ¬
This should allow for a thorough test."

EDIT$(1)  = t1Str + CHR$(13)
EDIT$(1, _maxInt, _maxInt) = CHR$(13) + t2Str

DO
HANDLEEVENTS
UNTIL 0
END

'---------- END CODE ------------