RichTextBox Formatting Help
-
Ok, I know it is long... In an OpenFile() routine I am trying to change the color on the text of a file at a specific position, and do this prior to it loading into a RichTextBox.Text property. I can do this with the findSequenceNumbers() routine below; If I do it after it loads by using the findSequenceNumbers() routine it takes about 3 - 4 minutes dpending on the file size as it scrolls thru the text box. I want to perform this formatting using the RichTextBox.RTF property however I do not understand it, nor do I understand where I would fit it in. I did create the file and save it after I formatted it as an RTF document and when I open it in a text editor this is what is at the top; {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} {\colortbl ;\red0\green0\blue139;} \viewkind4\uc1\pard\lang1033\f0\fs20 A formatted line with the color change looks like this; 2007/08/13 14:00:03 [144007]ARES_EINDICATION 010.050.016.010 420.1.01 (6901) RX 68 bytes 69 01 26 02 28 A4 AA 20 76 96 51 44 50 76 08 45 46 00 34 02 02 C7 88 \cf1\b 01\cf0\b0 C7 88 AA 50 76 08 45 46 20 76 96 51 44 D7 07 08 0D 0D 3B 3B 00 00 10 06 0A 06 06 06 06 06 06 0A 0A 06 06 06 06 0A 0A 9B 67 5F 91 F8 \par ------------------------------------------- private void OpenFile() { try { if (OpenFileDialog.ShowDialog() == DialogResult.OK) { string strExt; strExt = System.IO.Path.GetExtension(OpenFileDialog.FileName); strExt = strExt.ToLower(); if (OpenFileDialog.FileName == "") { return; } switch (strExt) { default: if (strExt == ".rtf") { rtbDoc.LoadFile(OpenFileDialog.FileName, RichTextBoxStreamType.RichText); } else { StreamReader rtfReader; rtfReader = new StreamReader(OpenFileDialog.FileName); rtbDoc.Text = rtfReader.ReadToEnd(); rtfReader.Close(); rtfReader = null; rtbDoc.SelectionStart =
-
Ok, I know it is long... In an OpenFile() routine I am trying to change the color on the text of a file at a specific position, and do this prior to it loading into a RichTextBox.Text property. I can do this with the findSequenceNumbers() routine below; If I do it after it loads by using the findSequenceNumbers() routine it takes about 3 - 4 minutes dpending on the file size as it scrolls thru the text box. I want to perform this formatting using the RichTextBox.RTF property however I do not understand it, nor do I understand where I would fit it in. I did create the file and save it after I formatted it as an RTF document and when I open it in a text editor this is what is at the top; {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} {\colortbl ;\red0\green0\blue139;} \viewkind4\uc1\pard\lang1033\f0\fs20 A formatted line with the color change looks like this; 2007/08/13 14:00:03 [144007]ARES_EINDICATION 010.050.016.010 420.1.01 (6901) RX 68 bytes 69 01 26 02 28 A4 AA 20 76 96 51 44 50 76 08 45 46 00 34 02 02 C7 88 \cf1\b 01\cf0\b0 C7 88 AA 50 76 08 45 46 20 76 96 51 44 D7 07 08 0D 0D 3B 3B 00 00 10 06 0A 06 06 06 06 06 06 0A 0A 06 06 06 06 0A 0A 9B 67 5F 91 F8 \par ------------------------------------------- private void OpenFile() { try { if (OpenFileDialog.ShowDialog() == DialogResult.OK) { string strExt; strExt = System.IO.Path.GetExtension(OpenFileDialog.FileName); strExt = strExt.ToLower(); if (OpenFileDialog.FileName == "") { return; } switch (strExt) { default: if (strExt == ".rtf") { rtbDoc.LoadFile(OpenFileDialog.FileName, RichTextBoxStreamType.RichText); } else { StreamReader rtfReader; rtfReader = new StreamReader(OpenFileDialog.FileName); rtbDoc.Text = rtfReader.ReadToEnd(); rtfReader.Close(); rtfReader = null; rtbDoc.SelectionStart =
Hi! Although I cannot really follow what you're trying to do, it seems as if a different way would be much easier: Create a (invisible)
RichTextBox
, load the RTF document into this box, find the lines you want to colorize using theText
property of the RTB, select them and then assign theSelectionColor
you want. Afterwards you can simply assign the invisible RTB'sRtf
property to the target RTB and you're done. If you really want to modify the RichText yourself, I suggest reading the Rich Text Format (RTF) Specification, version 1.6[^], but a quick scan through the introduction will show you that it's not as easy as you might think.Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! Although I cannot really follow what you're trying to do, it seems as if a different way would be much easier: Create a (invisible)
RichTextBox
, load the RTF document into this box, find the lines you want to colorize using theText
property of the RTB, select them and then assign theSelectionColor
you want. Afterwards you can simply assign the invisible RTB'sRtf
property to the target RTB and you're done. If you really want to modify the RichText yourself, I suggest reading the Rich Text Format (RTF) Specification, version 1.6[^], but a quick scan through the introduction will show you that it's not as easy as you might think.Regards, mav -- Black holes are the places where God divided by 0...
I have tried having the RichTextBox is both visible and invisible. No difference in the time it takes for the SelectionColor. It takes 3 -4 minutes regardless. My goal is to speed up the process for this. If I select all text and change it to BOLD it takes a second or two, if I change the color of all the text it takes the same time. Your right the RTF spec is not easy. Brian
-
Ok, I know it is long... In an OpenFile() routine I am trying to change the color on the text of a file at a specific position, and do this prior to it loading into a RichTextBox.Text property. I can do this with the findSequenceNumbers() routine below; If I do it after it loads by using the findSequenceNumbers() routine it takes about 3 - 4 minutes dpending on the file size as it scrolls thru the text box. I want to perform this formatting using the RichTextBox.RTF property however I do not understand it, nor do I understand where I would fit it in. I did create the file and save it after I formatted it as an RTF document and when I open it in a text editor this is what is at the top; {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} {\colortbl ;\red0\green0\blue139;} \viewkind4\uc1\pard\lang1033\f0\fs20 A formatted line with the color change looks like this; 2007/08/13 14:00:03 [144007]ARES_EINDICATION 010.050.016.010 420.1.01 (6901) RX 68 bytes 69 01 26 02 28 A4 AA 20 76 96 51 44 50 76 08 45 46 00 34 02 02 C7 88 \cf1\b 01\cf0\b0 C7 88 AA 50 76 08 45 46 20 76 96 51 44 D7 07 08 0D 0D 3B 3B 00 00 10 06 0A 06 06 06 06 06 06 0A 0A 06 06 06 06 0A 0A 9B 67 5F 91 F8 \par ------------------------------------------- private void OpenFile() { try { if (OpenFileDialog.ShowDialog() == DialogResult.OK) { string strExt; strExt = System.IO.Path.GetExtension(OpenFileDialog.FileName); strExt = strExt.ToLower(); if (OpenFileDialog.FileName == "") { return; } switch (strExt) { default: if (strExt == ".rtf") { rtbDoc.LoadFile(OpenFileDialog.FileName, RichTextBoxStreamType.RichText); } else { StreamReader rtfReader; rtfReader = new StreamReader(OpenFileDialog.FileName); rtbDoc.Text = rtfReader.ReadToEnd(); rtfReader.Close(); rtfReader = null; rtbDoc.SelectionStart =
I don't have a quick and easy answer for you, but you probably weren't expecting one. I can offer a little help though, I hope. I have used RichTextBox controls a little bit, but what I was doing was writing a simple "report, " saving it to a file, sending it to a client application that then displayed the file in a RichTextBox control. Anyway, you said you wanted to use the .Rtf property. I didn't use it, but as I understand it, you'd need to load your rtf-formatted text into a string object, then
string rtfString = /* some magic loading */ ; rtbDoc.Rtf = rtfString ;
What I did was:richTextBox.LoadFile ( path ) ;
I don't think that the .Rtf property is going to be much help, though. Here are my assumptions about things you've done so far: You have data in a text-based file captured from a device or other source. You opened a data file in Wordpad and found the text of interest. You changed the interesting text to bold and blue. You saved the result as .rtf file. A portion of that file is shown in your post.\cf1\b 01\cf0\b0
This portion of that text means color foreground #1 from color table, bold, 01 (which is the interesting text), color foreground #0 (back to default color), bold off. The color table is defined on line 2:{\colortbl ;\red0\green0\blue139;}
I don't think that the 3 - 4 minute delay really has anything to do with rtf issues. I think that's time taken forfindSequenceNumbers
to parse the strings. I'd try putting some debug statements before and after a call to it. Find out how much time it is taking. If my first assumption above is correct, my approach would be to process the data file a line at a time, looking for the tokens as seen infindSequenceNumbers
and embedding the rtf instructions\cf1\b
and the like in the lines. Of course you'd also have to put the other rtf stuff in your StringBuilder or whatever. Not sure if that's really going to speed things up or not, it's just another way to skin the cat. Hope this helps a little. BDF -
I don't have a quick and easy answer for you, but you probably weren't expecting one. I can offer a little help though, I hope. I have used RichTextBox controls a little bit, but what I was doing was writing a simple "report, " saving it to a file, sending it to a client application that then displayed the file in a RichTextBox control. Anyway, you said you wanted to use the .Rtf property. I didn't use it, but as I understand it, you'd need to load your rtf-formatted text into a string object, then
string rtfString = /* some magic loading */ ; rtbDoc.Rtf = rtfString ;
What I did was:richTextBox.LoadFile ( path ) ;
I don't think that the .Rtf property is going to be much help, though. Here are my assumptions about things you've done so far: You have data in a text-based file captured from a device or other source. You opened a data file in Wordpad and found the text of interest. You changed the interesting text to bold and blue. You saved the result as .rtf file. A portion of that file is shown in your post.\cf1\b 01\cf0\b0
This portion of that text means color foreground #1 from color table, bold, 01 (which is the interesting text), color foreground #0 (back to default color), bold off. The color table is defined on line 2:{\colortbl ;\red0\green0\blue139;}
I don't think that the 3 - 4 minute delay really has anything to do with rtf issues. I think that's time taken forfindSequenceNumbers
to parse the strings. I'd try putting some debug statements before and after a call to it. Find out how much time it is taking. If my first assumption above is correct, my approach would be to process the data file a line at a time, looking for the tokens as seen infindSequenceNumbers
and embedding the rtf instructions\cf1\b
and the like in the lines. Of course you'd also have to put the other rtf stuff in your StringBuilder or whatever. Not sure if that's really going to speed things up or not, it's just another way to skin the cat. Hope this helps a little. BDFYour assumptions were correct. Puts things in a different perspective, and I appreciate the answer. ' Brian