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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search-and-Replace in Rich Text

Status
Not open for further replies.

dqrose

Technical User
Mar 17, 2006
24
GB
This one follows on from an earlier query ("Colored Text in TextBox"), but I have a clearer idea of what I'm trying to do now, so I'll start a new thread.

I have a VB5 application containing a RichTextBox, into which I've imported a Rich Text document. That document contains some words which are in different colors to the others, for example:

"This is a keyword, whereas this is a comment."

I want to edit this document within the RichTextBox using a selection of buttons that I'll create, which will perform a number of predefined operations. These operations include such things as turning all instances of the word "comment" blue, stripping out all the tabs, inserting carriage-returns after all the semicolons, and things like that.

How do I perform such search-and-replace operations on rich text?

I've tried duplicating the VB functions that MS Word writes within a user-defined macro when I run a search-and-replace operation there, but I can't relate the VB code that is created there to what I am trying to do here. I've also tried using the Instr function to perform the same tasks with some success, but more often than not I end up making the file uninterpretable as rich text.

(For the record, I'm writing an application that will automatically tidy up code written in another language. I know I could probably do it entirely with MS Word macros, but there are other aspects of the problem which make it preferable to write it as a stand-alone VB application.)

Many thanks for any help received.
 
A good starting point might be thread222-1133098, the use of Regular Expressions in that thread and the link strongm's posted should be of great interest to you. Plus the solution and question are pretty clos to what you are after.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
It does indeed help: this looks extremely encouraging. Many thanks!
 
I have two last questions, after which I reckon I'll have answers to everything I need.

Firstly, I want to be able to apply the search-and-replaces I've described above just within a portion of the text that I may have highlighted by dragging the cursor over it, for example to select just one paragraph. How do I check to see if any particular word is contained within such a selection?

Secondly, I want to set the font and font size of any document that is loaded into the RichTextBox to Courier 8-point, but without changing any of the color, bold or italic settings already contained within the document. Can this be done?

My apologies if these are trivial questions, but I'm unfamiliar with many of the properties of a RichTextBox, and I don't know which ones I need to access.

Finally, MANY thanks for the help I've received to date - it's been phenomenally instructive, and has saved me days of head-scratching.
 
Hi,

As a start for the highlighted text question you can look into the .SelText property of the RichTextBox. This will give you the text you have currently selected (allowing you to only use what is selected).

With regard to the second question, the only way i can currently see to keep the colouring but change the font and size would be to edit the .TestRTF property of the Textbox.
I've tried this and it seems to work fine. I'd have a look at the .TextRTF and you can pick out what needs to be changed (all of the font and colour settings are at the top).
I'd recommend looking for the font names and fs for the size. E.G. for :

Code:
[blue]SELECT * FROM TABLE1[/blue] WHERE NAMES = [red]'HarleyQuinn'[/red]
The TextRTF output would be:
Code:
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Courier;}{\f1\fnil\fcharset0 Tahoma;}}
{\colortbl ;\red0\green0\blue240;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\lang2057\f0\fs20 SELECT\cf2  \cf1 *\cf2  \cf1 FROM\cf2  TABLE1 \cf1 WHERE\cf2  NAMES \cf1 =\cf2  \cf3 'HarleyQuinn'\cf0\f1\fs17 
\par }
This might be a rather poor explanation but here goes.

The colortbl; tag contains the colours for the document (in this case Blue (\red0\green0\blue240), Red (\red255\green0\blue0) and Black (\red0\green0\blue0) all seperated by a ;

They are used in the actual text where there corresponding tag appears (e.g. \cf1 for Blue and \cf3 for Red)

The fonts are in the fonttbl tag and each have a key (e.g f0, f1) They are called in the same way as the colour tags. e.g.
Code:
f0\fs20
at the start of the document specifying the font it is using and then the size after seperated by a \ (where you need to be looking, beware fs20 isn't fontsize 20 so changin it to 8 won't work!)

I've rambled on for ages now so hopefully some of what I've written will be of help to you!

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
HarleyQuinn said:
beware fs20 isn't fontsize 20 so changin it to 8 won't work!
I didn't mean that you couldn't change the fontsize there [smile]
You can of course change it (to fsxx, xx being the size) and it will change the size of the font but not necessarily to what you are expecting (fs8 is absolutely tiny). You will be able to get the correct size with a little bit of tinkering around [wink]

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
>the only way i can currently see to keep the colouring but change the font and size

If we're going to stick with the (limited) VB RichTextbox interface, then I believe that .SelFontName and .SelFontSize should do the trick

However, I much prefer using the dramatically more flexible TOM interface that I illustrated (obscurely) in the thread linked to by HarleyQuinn, which gives you a kind of cut-down Word
 
strongm,

Good call on the .SelFontSize and .SelFontName properties, they do indeed do the trick and I'd overlooked them in my reply [blush].

Having tried the code you posted on the TOM interface I can see why you would prefer that, it's nice [smile]

Thanks for helping both dgrose and myself [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
> it's nice

Certainly is - and it demonstrates just how much functionality MS chose not to implement for normal VB programmers ..
 
>>> "This is a keyword, whereas this is a comment."

Blast my color-blindness!!!!! :p
 
My work's progressing brilliantly, thanks to everybody here. You've saved me ages!

One very quick final question (honest!): having loaded an RTF file into a RichTextBox, how can I select the whole document (i.e. as if I had hit Control-A when editing it in a normal word processor)? Does one of the properties of the box control this, or do I have to simulate typing Control-A from within the program? If so, how?
 
strongm - I can see what you mean.

dgrose - You can use the .SelStart and .SelLength properties of the RTB, e.g.
Code:
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 65535
That will select all of the text in your RTB.

If you're doing this from a button click then you may well have to add in a RichTextBox1.SetFocus so the user can see that the text is selected.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top