Click to See Complete Forum and Search --> : [RESOLVED] Appending colored text to a richtextbox?


Traps
February 13th, 2009, 12:46 AM
The thread title is pretty self explanitory. I'd think this would be easy, but my approaches havent worked. Any ideas?

I have an RTB that is filled line by line via a console application's standard output stream. I want to color parts of the line of text before it gets appended.

Here is the line of code I have that appends the stream output line to the RTB:

Dictionary_RTB(sender).Text += Dictionary_Output(sender);

That line of code is essentially the same as:

RichTextBox1.Text += (string) OuputString;


Any ideas here?


If I can get the character position of the first character in the last line of the RTB, that would be perfect.

dannystommen
February 13th, 2009, 02:05 AM
Take a look here

http://www.codeguru.com/forum/showthread.php?p=1791268#post1791268

Traps
February 13th, 2009, 02:09 AM
Didnt see your post. Until now, I think that might do it. At least its not losing the formatting now by using the append... Thanks, this should take care of it. I'll be sure to rate ya for it, and mark the post answered once I confirm it.

Traps
February 13th, 2009, 02:35 AM
Well, the formatting sticks, IF it gets applied, so its still not working correctly. Here's my code:

private void UpdateText(clsServer sender)
{
try {
Dictionary_RTB(sender).AppendText(Dictionary_Output(sender));

Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexFromLine(Dictionary_RTB(sender).Lines.Count - 1);
Debug.Print(Dictionary_RTB(sender).SelectionStart);
MessageBox.Show(Dictionary_RTB(sender).TextLength);
Dictionary_RTB(sender).SelectionLength = 5;
Dictionary_RTB(sender).SelectionColor = Color.Red;

Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).TextLength - 2;
Dictionary_RTB(sender).ScrollToCaret();
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}


This line:

Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexFromLine(Dictionary_RTB(sender).Lines.Count - 1);

Returns the character position at the end of the textbox, or in otherwords, equals the .TextLength value..... Thats not what I would infer would be the return result for something that says GetFirstCharIndexFromLine....... This is at least part of the problem, if not the whole problem..... Why doesnt that line work like it should?


I tried this (much nicer code):

Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine()

but it returns the same values. For example, the first time I hit that line when running my program, it returns an index of 37. There is only 1 line in the RTB, and that line is 37 characters long. It should be returning 1!

Traps
February 13th, 2009, 11:23 PM
OMG, why doesnt this work, please help me.

Dictionary_RTB(sender).AppendText(Dictionary_Output(sender));
string tmp0 = Dictionary_Output(sender);

// This text formatting code is currently under testing. IT DOES NOT WORK.
Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine();
int tmp = Dictionary_RTB(sender).SelectionStart;
Debug.Print(tmp); <<<<<< BREAKPOINT HERE!!!

tmp0 = "Microsoft Windows [Version 6.0.6001]"
tmp = 37

this is the FIRST time that this code is hit when I run my program. Prior to that, the RTB text is empty.

NOOOOO! tmp should = 1 or 0 ..... but not 37!!!!


PLEASE, WHY?

Traps
February 14th, 2009, 12:05 AM
I dont know why, but I GOT IT!

Dictionary_RTB(sender).AppendText(Dictionary_Output(sender));
string tmp0 = Dictionary_Output(sender);

// This text formatting code is currently under testing. IT DOES NOT WORK.
Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).TextLength - 2;
Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine();
int tmp = Dictionary_RTB(sender).SelectionStart;
Debug.Print(tmp);



Whatever it works, thats all the counts for me right now.