470,864 Members | 1,948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,864 developers. It's quick & easy.

RichTextFormat to PlainText

I have been working on a project to Search and Replace text within files. The
project includes automatic file backups and date by date file restores. I use
a richtextbox to display the text with color formatting where search strings
have been found. This way I can highlight multiple search finds at the same
time, unlike a textbox.
To be efficient I load the files that have found strings into an array and
display the names of these files in a listbox. Once the listbox is clicked
the file loads from the appropriate index in the array into the richtextbox
and formats the text where strings have been found. If any changes are made
to the richtextbox via the automatic replace or a manual change they are
saved back to the array and marked as changed so that they can be saved back
to the file when the users chooses. Importantly they are saved back to the
array as richtext and no longer plain text and thus the problem arises.
I am aware that richtextboxes have a savefile function that allows them to
save their info as PlainText but this seems to be the ONLY way to get data
out of them as plain text and I don’t want to rush the user to save each file
as they make changes. Also I think that it is inefficient to load the array
back into the richtextbox for each file as the save process is completed when
there could be potentially hundreds of files to be saved. For the same reason
I’m not sure opening hundreds of child forms would be any better. I'm trying
to avoid using too much resources but I have found no other way to get
richtextformat to save as plain text without the funky formatting. Of course
convert types do no address this matter since being richtextformat is not an
argument of being a string or not.

I have seen other control types that can do much more sophisticated
highlights than even a richtextbox has to offer. For example just bring up a
found search topic in .net and you will see highlighted text etc. I've
attempted to set something up via HTML but I'm not too savvy with that and
after a futile attempt thought that I might ask here before investing loads
of time researching if HTML was even the way to go. After all my interface
must be able to accept changes by the user and convert that back to plain
text without any formatting of any kind introduced to the file.

I'm just a sole developer lost in the universe of .net
Somebody please help. There has to be a better solution to this problem.

Apr 3 '06 #1
5 1101
Hello, AWenser,

Re: "Importantly they are saved back to the array as richtext and no
longer plain text and thus the problem arises."

I don't understand why you need to do this. Why not just save them back
to the array using the Text property?

Cheers,
Randy
AWesner wrote:
I have been working on a project to Search and Replace text within files. The
project includes automatic file backups and date by date file restores. I use
a richtextbox to display the text with color formatting where search strings
have been found. This way I can highlight multiple search finds at the same
time, unlike a textbox.
To be efficient I load the files that have found strings into an array and
display the names of these files in a listbox. Once the listbox is clicked
the file loads from the appropriate index in the array into the richtextbox
and formats the text where strings have been found. If any changes are made
to the richtextbox via the automatic replace or a manual change they are
saved back to the array and marked as changed so that they can be saved back
to the file when the users chooses. Importantly they are saved back to the
array as richtext and no longer plain text and thus the problem arises.
I am aware that richtextboxes have a savefile function that allows them to
save their info as PlainText but this seems to be the ONLY way to get data
out of them as plain text and I don’t want to rush the user to save each file
as they make changes. Also I think that it is inefficient to load the array
back into the richtextbox for each file as the save process is completed when
there could be potentially hundreds of files to be saved. For the same reason
I’m not sure opening hundreds of child forms would be any better. I'm trying
to avoid using too much resources but I have found no other way to get
richtextformat to save as plain text without the funky formatting. Of course
convert types do no address this matter since being richtextformat is not an
argument of being a string or not.

I have seen other control types that can do much more sophisticated
highlights than even a richtextbox has to offer. For example just bring up a
found search topic in .net and you will see highlighted text etc. I've
attempted to set something up via HTML but I'm not too savvy with that and
after a futile attempt thought that I might ask here before investing loads
of time researching if HTML was even the way to go. After all my interface
must be able to accept changes by the user and convert that back to plain
text without any formatting of any kind introduced to the file.

I'm just a sole developer lost in the universe of .net
Somebody please help. There has to be a better solution to this problem.

Apr 4 '06 #2
If you mean that I should set my array value to the richtextbox.text property
that is what I am doing. Rich text format is still concidered text however.

arrFile(2, iSelect) = rtbFound.Text
Perhaps I should be formatting my array differently? It holds various data
like the file path as string, file data as string, isEdited as boolean.
"R. MacDonald" wrote:
Hello, AWenser,

Re: "Importantly they are saved back to the array as richtext and no
longer plain text and thus the problem arises."

I don't understand why you need to do this. Why not just save them back
to the array using the Text property?

Cheers,
Randy
AWesner wrote:
I have been working on a project to Search and Replace text within files. The
project includes automatic file backups and date by date file restores. I use
a richtextbox to display the text with color formatting where search strings
have been found. This way I can highlight multiple search finds at the same
time, unlike a textbox.
To be efficient I load the files that have found strings into an array and
display the names of these files in a listbox. Once the listbox is clicked
the file loads from the appropriate index in the array into the richtextbox
and formats the text where strings have been found. If any changes are made
to the richtextbox via the automatic replace or a manual change they are
saved back to the array and marked as changed so that they can be saved back
to the file when the users chooses. Importantly they are saved back to the
array as richtext and no longer plain text and thus the problem arises.
I am aware that richtextboxes have a savefile function that allows them to
save their info as PlainText but this seems to be the ONLY way to get data
out of them as plain text and I don’t want to rush the user to save each file
as they make changes. Also I think that it is inefficient to load the array
back into the richtextbox for each file as the save process is completed when
there could be potentially hundreds of files to be saved. For the same reason
I’m not sure opening hundreds of child forms would be any better. I'm trying
to avoid using too much resources but I have found no other way to get
richtextformat to save as plain text without the funky formatting. Of course
convert types do no address this matter since being richtextformat is not an
argument of being a string or not.

I have seen other control types that can do much more sophisticated
highlights than even a richtextbox has to offer. For example just bring up a
found search topic in .net and you will see highlighted text etc. I've
attempted to set something up via HTML but I'm not too savvy with that and
after a futile attempt thought that I might ask here before investing loads
of time researching if HTML was even the way to go. After all my interface
must be able to accept changes by the user and convert that back to plain
text without any formatting of any kind introduced to the file.

I'm just a sole developer lost in the universe of .net
Somebody please help. There has to be a better solution to this problem.

Apr 4 '06 #3
Hi,

AFAIK, the Text property of a RichTextBox doesn't output RichText, it
simply returns the text without the RTF codes. The RTF property is the
one that should return the text *with* the RTF codes. I think that's
what Randy was questioning, as well.

So, how are you determining that the data is saved to the array as
RichText and not simple text ?

Regards,

Cerebrus.

Apr 4 '06 #4
I have determined that the text has changed by several different methods:
1: just after loading the rtf box with the string from the array I find that
the .text.compare property finds these two strings different. it has already
changed the text in the control without any user intervention. also if you
place a break here and watch the two strings before and after you will see
they are a different length because of the RTF formatting.
2: by opening a writestream and saving this data in the array to the file. I
have tried using many different commands and properties to write to a file
but they all turn out with the same result to me. The first thing you will
see are square ascii characters where the carraige return used to be. Also
when testing you need to open up in Notepad or something else that does not
support RTF or you will not see the anomaly.

I have observed that this happens on edited text within the rich text box.
It does not overwrite with different ascii codes in this pattern when the
text is unmodified.

If you are curious to see then you can make a simple version of this by
creating a form with a richtextbox. Use the FSO to write from the control to
the file as this is the preffered method for me. Using the rtf.SaveFile(as
plaintext) property is a last resort and I am trying to figure out how not to
use that.

For clarification I have never used the .rtf property.

Thank you for the replies.

"Cerebrus" wrote:
Hi,

AFAIK, the Text property of a RichTextBox doesn't output RichText, it
simply returns the text without the RTF codes. The RTF property is the
one that should return the text *with* the RTF codes. I think that's
what Randy was questioning, as well.

So, how are you determining that the data is saved to the array as
RichText and not simple text ?

Regards,

Cerebrus.

Apr 4 '06 #5
It doesn't appear that anyone has an answer readily available for this quirky
little problem or why. I know the basic concept of how RTF works and I know
that it is placing a few different characters that an RTF reader understands
as carraige returns, but I don't know enough to try to create my own
algorithm to replace these RTF style carraige return characters with the
plain text style characters. I would rather not assume since everyone knows
what kinds of trouble that can lead to.

I've had the idea that since .savedata(as plaintext) does work, meaning it
doesn't leave those lovely ascii characters in my text files and as long as I
still have the changed data in the richtextbox before I unload it, I will
have the richtextbox perform its savedata function and make a temporary file
for each edited file and place them in the base application folder. Then when
the user comfirms that all changes are to be saved these files can be
reloaded back into the array and I can continue through my savetime
enumeration. Otherwise I'll discard them. Symptoms solved, however the true
cause is not...
Apr 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ajay Brar | last post: by
6 posts views Thread by Joerg Battermann | last post: by
2 posts views Thread by genojoe | last post: by
7 posts views Thread by Luca Villa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.