syntax highlighting revisited

P

Patrick Porter

I am looking at followin the suggestion made by Larry on my last post. (his
reply is on the bottom) it seems that you can't directly mess with the rich
text header text....specifically the addtion of the colortable (which
defines which colors can be used)

rtbCode.Rtf.Insert(10,"{\colortbl
;\red255\green0\blue0;\red0\green0\blue255;}")

does nothing.
neither does rewrting the entire header each time.... why is this? In order
for me to add color, i have to have a color table written at the beginning.
once again i turn to you all for help.

thanks, patrick
-------------------------------------------------------->
(Larrys post:
The RichTextBox offers a property called Rtf which gets/sets the actual
rich text as a String. If you look at this you will see stuff like
this:

this is normal text
\b this is bold
\i this is italic

I'm not sure what the exact syntax is, but basically this is how rich
text works - by inserting formatting instructions into the regular text
(like a primitive form of HTML if you like). You could try obtaining
the string from .Rtf, parsing it for 'ax', inserting the appropriate
formatting codes, then writing it back to .Rtf. This should avoid the
problems you get when you change the Selection.

To find out what the right control codes are, use WordPad to create a
..rtf file and examine that file using Notepad.
 
L

Larry Lard

Two issues here:

First, although 'Insert' might look like it 'does something', it is in
fact a function not a statement, so what you actually need to do is
something like

rtbCode.Rtf = rtbCode.Rtf.Insert( ... 'whatever

Second, a little playing around seems to suggest that the rich text box
only retains in the \colortbl those colours that are actually USED. So
if you just insert a \colortbl, but all your text is still the default
colour, the \colortbl won't be stored. But if you actual colour some
text with \cf1 ... \cf0, then colour 1 will be preserved. So what you
need to do in order to carry out your highlighting is this:

- Get the Rtf string
- If there's already a \colortbl, leave it, otherwise insert one
- Put the \cf colour tags around the text you want to highlight
- Write back the Rtf

Watch out for the Replace method of String; like Insert, it is a
function not a statement so you have to do stuff like

s = s.Replace("old", "new")

rather than the 'intuitive'

s.Replace("old", "new")

That one used to catch me in VB6 all the time.
 
P

Patrick Porter

thanks again. I still have the problem using both the replace and insert
functions.....it doesnt work.

Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

nothing even

rtbcode.rtf.replace("ax","bx") doesnt work.

i hate to keep bugging the group, but you have been so helpful.

thanks,

patrick

---> some of the code <----

code = rtbcode.rtf

For i = 0 To results.Length - 1

Dim d As Integer = match(i).Index

results(i) = match(i).Index

s = register.Item(x).ToString()

Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

rtbCode.Rtf = code

Dim str As String = rtbCode.Rtf

Next



------> end code <----
 
L

Larry Lard

Methods called Replace are *functions* that *return* values. They don't
*change* their input values. After the line of code

rtbcode.rtf.replace("ax","bx")

rtbcode.rtf will be exactly the same as it was before, since you have
done nothing with the return value from the function. VB.NET is perhaps
slightly remiss in not warning you that you are doing this; although it
might get annoying to get warned every time a function return value is
discarded.

Anyway, if you want to change the value of a string you must assign the
new value to it: so not

Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

but rather

code = Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

and so on.



Patrick said:
thanks again. I still have the problem using both the replace and insert
functions.....it doesnt work.

Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

nothing even

rtbcode.rtf.replace("ax","bx") doesnt work.

i hate to keep bugging the group, but you have been so helpful.

thanks,

patrick

---> some of the code <----

code = rtbcode.rtf

For i = 0 To results.Length - 1

Dim d As Integer = match(i).Index

results(i) = match(i).Index

s = register.Item(x).ToString()

Regex.Replace(code, "\\cf[01].* " & s, "\cf2" & s)

rtbCode.Rtf = code

Dim str As String = rtbCode.Rtf

Next



------> end code <----

Larry Lard said:
Two issues here:

First, although 'Insert' might look like it 'does something', it is in
fact a function not a statement, so what you actually need to do is
something like

rtbCode.Rtf = rtbCode.Rtf.Insert( ... 'whatever

Second, a little playing around seems to suggest that the rich text box
only retains in the \colortbl those colours that are actually USED. So
if you just insert a \colortbl, but all your text is still the default
colour, the \colortbl won't be stored. But if you actual colour some
text with \cf1 ... \cf0, then colour 1 will be preserved. So what you
need to do in order to carry out your highlighting is this:

- Get the Rtf string
- If there's already a \colortbl, leave it, otherwise insert one
- Put the \cf colour tags around the text you want to highlight
- Write back the Rtf

Watch out for the Replace method of String; like Insert, it is a
function not a statement so you have to do stuff like

s = s.Replace("old", "new")

rather than the 'intuitive'

s.Replace("old", "new")

That one used to catch me in VB6 all the time.


(which
In
order
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top