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!

Highlight a text in Memo field 3

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
508
Brasil
Hello colleagues!

I want to expose a question in a particular mode:

Last year I talk to a Mrs.Mariana, who is secretary of a firm I do not remember now, and I need to contact that firm tomorrow.

I have a file with hundreds of records and in the memo field of one of the records I registered 'Mariana' word. So it was easy to find that register, as I have a program which can search which records have the text I typed (%&Mytext%, which has 'Mariana') to search operation. It works very fine.

But I would like to highlight the text searched, like this:

TelaCadastro_zjid6q.jpg


Is it possible to have the searched text highlighted? (Because some records have tens of lines in the memo field).


Thank you,
SitesMasstec
 
Not in an editbox or any fopro Foxpro control, you have to use an OLE control, capabale to display RTF format, for example.

Chriss
 
Hi SitesMasstec,

You can realize this text highlight for both a native VFP Textbox and a native VFP Editbox

You have to know the start position (x) and the length (y) of the highlight text:


Textbox is named Text1

Code:
WITH THISFORM.Text1
    .SelStart  = x
    .SelLength = y
    .SetFocus
ENDWITH


Editbox is named Edit1

Code:
WITH THISFORM.Edit1
    .SelStart  = x
    .SelLength = y
    .SetFocus
ENDWITH


Regards, Stefan
 
Stefan is right, I forgot about that, but this can't highlight the search term more than once within the text.

So in the end only if that's enough for you, it'll be a solution.

Chriss
 
Using a RichTextBox, for which you have the license to distribute it. too, there is a Find method, that you could call and that'll highlight the found places.

Chriss
 
Hi Chris,

I agree with You:

the Microsoft RichTextBox Control Richtx32.ocx is much more comfortable !!! there is no need to know the start position and the length of the searched string

the use of the Microsoft RichTextBox Control Richtx32.ocx is free for VFP developers

the latest version of Richtx32.ocx is file version 6.1.98.39 = product version 6.01.9839

Regards, Stefan
 
On the other hand, you can make use of selstart/sellength and make a feature out of the problem by adding in a navigation to the next/previous found instance. Just like the CTRL+F feature within browsers, for example. I still find it more comfortable to already have all found places highlighted. But navigation on top of that is also what you get in a browser. Or in CHM help files CTRL+F feature.

All in all worth considering the Richtext control and adding in the navigation, especially if found places are at great distances and not in sight at the same time. That said, it's not a trivial task to scroll to a place and move it in sight, so you still might only do that as optional add-on feature.

Chriss
 
Dear colleagues:

As I have never used the RichTextBox Control Richtx32.ocx, I am going to study about it. I think I will need several days to read some books I have from Hentzenwerke & others to accomplish the task.

But for now I used the method presented by Stefan, and it works, even if it shows only the first occurence of the desired text. It is ok for me, BUT... the problem is the X (start). The Y (lenght of the text) is easy: Y=LEN(MyText).

The only problem is to find the X value, the Start point of the first occurence of the desired text (in the example in my first post above, 'Mariana').

Just to test, I put the Start position as 70:
Code:
WITH THISFORM.txtCobs2
    .SelStart  = 70     && Just to test
    .SelLength = LEN(MyText)
    .SetFocus
ENDWITH

And this is the result:
TelaCadastro2_dwkwei.jpg


The X is que question. Maybe there will be a lot of calculations to find out the start position or maybe VFP 9 has a command to find out this position of the text (in this case, "Mariana", rounded by red lines).


Thank you,
SitesMasstec
 
Well, I did this and got the result I expected:

Code:
.SelStart = (AT(LOWER(MyText), LOWER(THISFORM.txtCobs2.Value)))-1

Text in memo field:
---------------------------------------------------------------
...
12/10/2016 Enviado e-mail sobre reunião p/ sr.Bartholomeu.
idem Confirmada reunião pela secretária Mariana.
...
---------------------------------------------------------------

I had to put -1 in the end of the code, because, without putting -1, if the text I type is "Mariana" the highlighted text was "ariana."

I haven't found out why!


Thank you,
SitesMasstec
 
well, AT() is 1-based,, SelStart is 0-based. What does that mean? Well, AT() gives the position within a string, starting at 1, just as you naturally would count, SelStart=0 means the start of the selection is left of the leftmost character, 1 means the selection starts after the leftmost character, so that start position is 0-based. One of the ver few things in VFP that is 0-based.

You're right to have this remaining question, as all things in VFP in principle are 1-based, starting to count at 1, the first array element in VFP also is arrayname[1], not arrayname[0].

To keep it short, There is no mathematical explanation to this, it's a mere decision of the VFP design what 0 or 1 means for SelStart, and the VFP developers here chose 0-based instead of the usual 1-based logic.

Chriss
 
Hi SitesMasstec,

You can use ATC() instead of AT()

ATC() is NOT case sensitive

using ATC() means, there is no need for LOWER() ... or UPPER()

Code:
.SelStart = ( ATC( MyText, THISFORM.txtCobs2.Value ) ) -1

Regards, Stefan
 
A consideration to make, when you go for selecting text to highlight it: It is selected text, just like any other selection, it's not just a visual highlight. That means if the user types anything that will overwrite the selection, DEL or backspace just deletes it, etc. Just like with any normal selection. So either you make the editbox readonly to avoid such problems, or you live with these consequences. It's not the world's end, all actions are also just as normally reversable, too, with CTRL+Z. It may just not be as apparent to users as it is when they would manually select the text.

So a richtextbox or conversion to HTML (as simple as keeping the text as is or putting it into <p></p> tags) would be options that allow a purely visual highlighting without consequences for the editing of the text. When ou go for HTML, you surely find many solutions for text highlighting done Javascript, which you can embed within a webbrowser control, too. And you gain even more features as RTF offers.

Chriss
 
Hello colleagues!

This morning I had good and bad news...lol

Stefan: I changed AT(... for ATC(... and it is fine. VFP has lots of nice functions!

Chriss: because of your alert about a user doing a simple keystroke on a highlighted text, it will desappear, I tested it... you are right: I will have to abandon AT/ATC for my specific program as soon as I understand how to use RichTextBox Control Richtx32.ocx. I will dive into my books.


Thank you,
SitesMasstec
 
Well, if you don't need to give the user the ability to edit the text, just set the editbox to readonly and that problem also is solved.

Chriss
 
If, on the other hand, you want the user to be able to edit, that makes the Richtext box a good alternative easier than using HTML for display and also editing.

I haven't tried in detail, but if you edit in a Richtext control while find results are highlighted, that will work, as highlights are not selections but just a change of the background color.

Chriss
 

Sometimes the user have to type some text in this memo field, so it must NOT be always read-only.


Thank you,
SitesMasstec
 
Okay, then the readonly solution isn't sufficient. It also only solves the problem of not accidentally overwriting what the user thinks is just a highlight. Even if you set readonly .T. that still means any click anywhere sets the Selstart point and the text cursor appears at the clicked position, removing the selection. A selection is just a selection, no more, no less.

Chriss
 
I don't know if it is possible:

When the user HAS to type some text, I set the memo field as read-only = True and as soon as the user type anything or use an the arrow key to navigate down, I change read-only = False. As soon as the memo field open, I may put something like ON KEY (any key, indeed) thisform.read-only = False.

I will have to test, I am dreaming...


Thank you,
SitesMasstec
 
You could do things like that as you also have keypress event and can use the central form keypress with form.keypreview.

It will not change the behavior to overwrite the selection, though.

If you select text, the purpose normally is to copy it with CTRL+C, to cut it with CTRL+X, or to delete it with DEL. And all of these options also work, when you select text with code by using the SelStart and SelLength properties. If you didn't get it yet, the "Sel" of SelStart and SelLEngth stands for selection, so it's the SelectionStart and SelectionLength you set by code.

I don't know if you're not familiar with selections, I don't really believe it. It's the same as anywhere else, in Notepad or Word or even here in the box to type in tek-tips.

As I already said, even if a user would not expect typing to overwrite the selection, the usual user should be experienced enough to know that CTRL+Z always is there to undo.

It would be overengineered to react to the first keypress by unselecting the text. Some users might get the idea this is a selection anyway. Because if you look closely at the highlighted place, you do see the text cursor blinking there. Just because it's a selection, ont a highlight. VFP's editbox is as simple as a notepad text area, it only has one font and it offers no highlghts or coloring, not even bold text (only all bold text by choosing a bold font), it's purely a text editing box with no more detail features.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top