Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RTF.SELSTART/SELLENGTH/SELTEXT

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,

I am trying to reproduce the typical Find (text) behavior of any text editor in my VFP form with RichEdit ActiveX embedded. That is, user highlights some substring in the text currently displayed in the RichTextBox control, hits the Find button (VFP's native) and the next example of such substring is highlighted. Well, while the program does find next matching substring, it never highlights it.
Here's what I tried so far:
in the FORM.oRTF.SelChange event procedure, i.e.

Code:
WITH THISFORM
   .GetCursorLinePos() && Just another custom method I added to display the cursor's position
   .c_SelectedTxt = IIF(.oRTF.SelLength == 0, "", ;
                        SUBSTR(.oRTF.Text, .oRTF.SelStart + 1, .oRTF.SelLength) ;
                        ) && Add 1 position coz RichTextBox's counting is 0-based
ENDWITH
and it works as it's supposed to.
Then, in the FORM.cmdFind.Click event procedure, I have

Code:
LOCAL lcStr2Find, lnStrLen, lnFindStart, lnRet

IF THISFORM.oRTF.SelLength == 0
   RETURN	
ENDIF ()

lcStr2Find = THISFORM.c_SelectedTxt

IF EMPTY(lcStr2Find)  && Nothing to look for!
   RETURN
ENDIF (EMPTY(lcStr2Find)  && Nothing to look for!)

lnStrLen = LEN(lcStr2Find)
lnFindStart = THISFORM.oRTF.SelStart + 1 + THISFORM.oRTF.SelLength && Add 1 coz RichTextBox counting is 0-based
lnRet = THISFORM.oRTF.Find(lcStr2Find, lnFindStart)

IF lnRet < 0 && Sought substr was not found
   WAIT WINDOW NOWAIT [ Substring "] + lcStr2Find + [" was not found. ]
   RETURN
ENDIF (lnRet < 0 && Sought substr was not found)

WITH THISFORM.oRTF
   .SelStart = lnRet && lnFindStart
   .SelLength = lnStrLen
**   .SelText = lcStr2Find
ENDWITH

I tried it with and without assigning RTF's SelText property (in the line commented just above) - result was the same: the found instance of the sought substring was not highlighted.

The RichTextBox (or RichText Edit?) control is the one that came with Visual Studio 6.

The rest is VFP9SP2.

What am I doing wrong?


Regards,

Ilya
 
Here is the code I use for 'Find' in a RTF screen I use. It's in the "Keypress" event of the RTF control. Feel free to adapt or modify it if you want, it is rather simplistic and I haven't taken the time to do anything fancier with it.
The way it works is a CTRL+F does the initial find. If there is text highlighted on the form, the find dialog defaults to that, otherwise it uses whatever is currently in _CLIPTEXT. Pressing CTRL-G does a 'Find Again'.

Code:
*** ActiveX Control Event ***
LPARAMETERS keyascii

DO CASE 
   CASE keyascii = 6   &&... <Ctrl+F>
      IF EMPTY(_CLIPTEXT) .or. LEN(_cliptext) > 100
         _CLIPTEXT = ' '
      ENDIF 
      IF EMPTY(Thisform.rtf.oleRTF.selText)   &&... nothing selected
         Thisform.cText = INPUTBOX("Find what?", "Search", _CLIPTEXT)
      ELSE 
         Thisform.cText = INPUTBOX("Find what?", "Search", Thisform.rtf.oleRTF.selText)
      ENDIF 
      IF EMPTY(Thisform.cText)
         RETURN
      ENDIF 
      IF Thisform.rtf.oleRTF.find(Thisform.cText , This.selStart) < 0   &&... not found
         WAIT WINDOW "Not found." NOWAIT
      ENDIF 

   CASE keyascii = 7   &&... <Ctrl+G>
      IF Thisform.rtf.oleRTF.find(Thisform.cText, This.selStart + 1) < 0   &&... not found
         WAIT WINDOW "Not found." NOWAIT
      ENDIF 

   CASE keyascii = 27   &&... <Esc>
      Thisform.cmdClose.Click
      
   OTHERWISE 
   && NOP
   
ENDCASE 
   
RETURN

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Does the RichText ActiveX control support .SelStart and .SelLength properties? They're *Foxpro* properties but are they properties of the contained control? (I sort of doubt it.)

You may be barking up the wrong flagpole.
 
So, essentially, you're saying I need only to run oRTF.Find(), and if it finds it - RichEdit control should highlight that substring on its own?

Sorry, colleague, but I've it tried (commented out everything below

Code:
lnRet = THISFORM.oRTF.Find(lcStr2Find, lnFindStart)

line) - and it ain't highlighting either. And this highlighting is the whole point, coz this is what I'm trying to acieve: highlighting of the found substring. Does the highlighting holds on your RTF control, colleague Dave? (If it does - it means that "there's something rotten" in that control with the mouse events...)


Regards,

Ilya
 
Ilya,

In that case, you may need the Object keyword to differentiate between the VFP property and the property of the hosted control.

Code:
WITH THISFORM.oRTF.Object
   .SelStart = lnRet && lnFindStart
   .SelLength = lnStrLen
 
You said you're using a button to execute the find. What I've found is that in order for the text to show as selected, the RTF control has to have focus.
When you click the button, it is the object with focus, so you would need to set focus to the control after the code finds the text.
Or maybe you could try programmatically [sp?] changing the color of the selected text somehow.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Colleagues,

Please forgive this old senile fool with early Alzheimer disease symptoms - me :-( : RichEdit control has this bloody HideSelection property set to bloody True by bloody default, bleen! And I just now recalled about this bloody property [blush]... I set it to False - and my little text viewing form started behave the way it's supposed to.

Thank you for your help, and sorry for bothering y'al for no seriously good reason, mea culpa!

Regards,

Ilya
 
To Dan Freeman:

"If I only had a bloody nickel" - is it a song, or a verse, or - what? :) (Just curious, colleague.)

Regards,

Ilya
 
A slang saying.

"If I only had a nickel for every time I did that I'd be rich!"

It's a way of commiserating.
 
Oh, got it.
Sorry, I've been in this country (USA) for only 19 years and haven't yet heard this saying... "Me no spikah Inglish goot!" ;-)

Regards,

Ilya
 
You speak English better than many who were born here!

It's just a weird language and you haven't navigated all the turns yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top