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

How does the VB IDE do it?? 2

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I have a rich text box in which I need to colour certain strings with certain colours. Much like the way the VB IDE colours VB keywords etc.

I've done this by just stepping through all the text in the text box, checking for certain strings and once found using [color].SelText[/color] and [color].SelLen[/color] to color that chunk of text.

However, this takes a long time and needs the rich text box to be visibly scrolled through.

In the VB IDE it happens instantly when you copy un formatted text into the code window. How does it do it so efficiently? Would I have to do the formatting inside a string in memory and maybe add rich text formatting information? Or is there an easier way?

Cheers

elziko
 
Well, now you are getting a encountering a problem with how the RTF format works in a RichTextBox.

Basically it maintains a colour table, adding a new entry every time you set a selection to a new colour. Individual words and/or paras are then coloured by having a reference to that color table (eg \cf0 represents entry 0 in the table). By using your fixed replacement string, you are ALWAYS using \cf1 to determine the colour substitution, which will be the FIRST colour added to the colour table (i.e the colour defined by whichever of your two commands you run first).

Now, perhaps, you can see better why I used a dynamic method to build the replacement string.
 
Well thanks very much. I did wonder why you were creating the string like that. I've now had a look at the RTF format specification on MSDN.

I am now dynamically setting the colour table as well as the refrence to the table inside my string. I don't need the second rich textbox and it works very well.

My next step is to sort out possible commenting out of certain lines and portions of lines. For example in the C language code in between "/*" and "*/" would be commented and in VB the entire line after an apostrophe "'" would be commented. I have been reading up a little on regular expressions and I thought that if I used the function posted here (ReplacePattern) but replace "(" + patrn + ")" with:

"\{\S*\}" for the C example and
"\'\S*$" for the VB example then this would work.

However it doesn't and I think I might, once again, have missed the point of how this thing is working.
 
No - you haven't missed the point; you're just getting tangled up in the intracacies of Regular Epressions.

You cannot just replace "(" + patrn + ")" and expect the $1 trick to work. The brackets are an important part of the syntax, and indicate to the Regular Expression engine that patrn is a substitutable parameter (in fact you can have lots of these, which all get numbered sequentially, so you'd be able to play with $1, $2, $3, $4, etc.)

Getting your mind around Regular Expressions is NOT a quick job. I still have to go back to the documentation on mumerous occassions...
 
I could really do with a tiny bit more help. I wont be surprised if you're bored of all of this by now :) I have got a lot further in my understanding but have hit another brick wall. I'll just explain how far I have got.

I have used the following regular expression:

"(eggs|bacon)" etc

with 1$ in order to colour certain words.

I have now used two more regular expressions to colour text inbetween two strings (commenting as in C) and form one string to the end of the line (commenting as in VB). For this I have used the following two expressions:

VB:"('[\d\D]*?\n)"
C: "(/\*[\d\D]*?\*/)"

also used with 1$.

SO for the commenting the following text should be commented out after the apostophe:

"Would you 'like some butter with your bread?" so that the comment coloured text should read:

'like some butter with your bread? which it does. This works fine.

HOWEVER, if the commented words contain words that were coloured using the first expression then the commenting colour stops when in reaches them:

"Would you 'like some eggs with your bacon?"

The commented text here should read:

'like some eggs with your bacon?

but the only comment coloured parts are:

'like some "

I need to be able to comment text that has alreasy been replaced previously. The same thing happens within the C style commenting. I don't know if this is a Regular Expression issue or a RTF issue. Any ideas?

elziko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top