richTextBox string coloring

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

(I'm assuming this forum is for c# ?) how do I have a richTextBox highlight
everything inside the "<" and ">" tags, including the tags themselves, in
blue? I've searched and posted on another site, but I haven't found anything
that really answeres my question. thanks for your help,
Stephen
 
how do I have a richTextBox
highlight everything inside the "<" and ">" tags, including the tags
themselves, in blue?

You'll need to manually find each pair of braces, select them and the text
between them, and change the selected text colour.
The following very rough code should illustrate this:


richTextBox1.SelectAll();
richTextBox1.SelectionColor = Color.Black;
string text = richTextBox1.Text;
int start = 0;

while(true) {
start = text.IndexOf('<',start);
if(start < 0) break;
int end = text.IndexOf('>',start);
if(end < 0) break;
richTextBox1.Select(start,end - start + 1);
richTextBox1.SelectionColor = Color.Blue;
start = end;
}


Nathan
 
it ended up being I had to add two lines to the end of your code to deselect
the text, to keep from overwriting text and putting the cursor in the wrong
place. thanks for the help though, it worked!!! maybe within a week or so I
could figure it out, but do you think you could show me what the code would
look like to change the text color of anything in quotation marks inside of
the "<" and ">" tags to green. also, make it so the "<", ">", """, and """
are not colored green or blue? here are the two lines of code I added after
everything you had.

int textlength = richTextBox1.Text.Length;
richTextBox1.Select(textlength, 0);


thanks so much,
Stephen
 
Back
Top