Preserve carriage returns in RichTextBox control

B

Bill

Is there a way to prevent the RichTextBox (RTB) control (Framework
2.0) from removing carriage returns? When I set the .Text property,
it
converts all \r\n to \n.

I have Wordwrap set to false. I am setting the Text property to a
string that includes many lines, some end with \r\n, others with only
\n. AFter the user edits the text, it is saved to a file with the
Write method and I want that file to preserve the line endings that
were passed into the RTB. Therefore, I cannot do a global replace on
all the \n as a workaround. If it were not for the fact that the text
length exceeds the Textbox control limit, I would use that.

Bill
 
D

Dick Grier

I don't think you can do this directly.

Perhaps the best way is to mirror the data in the RTB to a string (or, an
array of bytes), prior to the file write. When it comes time to write it to
a file, use the string or array data. You can write the text to a string,
and do the search and replace, the write the data from the string or array.
This will be a little slower, of course...

Dick



--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
B

Bill

It would be a lot slower, as it would require comparing locating and
comparing each EOL character in the original string with the one from
the richtextbox.
But it would work. Any idea why the richtextbox strips the carriage
returns? was this an oversight by MS or intentional. I also know that
using win32 I can block that behavior, but I was hoping to have the
code entirely .net managed code.
 
D

Dick Grier

I don't know "why." I think that the RTF textbox in VB6 had the same
behavior, though.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

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