Removing RichText Formatting from RichTextBox in Visual C# - c#

I'm developing an advanced rich text editor in c# but have stumbled upon a problem that I can't seem to grasp.
I have been trying to let users save their documents as Text files (plain text). By using the following:
MyRichTextBox.SaveFile(filepath, PlainText);
But the problem is that when they view that file (which should have been saved as plain text) in Notepad, it shows up with all of the RTF formatting codes whish makes their documents unreadable. Does anybody know of any other way to remove formatting codes from a RichTextBox without having to do too much?
I mean, if there's going to be alot of work involved in finding a workaround to such a simple task, I might as well just create my own RichTextBox control from scratch. Which I'm sure would be very hard to do, but atleast I know that it'll work...
... Sorry for ranting on a little there, hehe...
Thanks All,
Jason.

This should do it:
String txt = MyRichTextBox.Text
File.WriteAllText(filepath, txt);
Alternatively, the way to get the RTF is:
String rft = MyRichTextBox.Rtf;
File.WriteAllText(filepath, rtf);

Have you tried:
RichTextBoxStreamType.PlainText
instead of:
PlainText

Related

A method to visually separate text lines in Richtextbox

I would like to know if there is an easier way to add any kind of visual separation like a line between text lines in a Richtextbox?
If not, is there another component that I can use in a Form that would be able to do this for text lines?
Thank you.
It has been a few years, but at one point I wrote a RichTextBox wrapper that ended up manipulating the underlying markup. It wasn't pretty and it wasn't for the faint of heart, but it did work. I ended up using it to highlight some text I believe.
The key thing you need to know is the specification for the RTF markup language: http://en.wikipedia.org/wiki/Rich_Text_Format should get you started.
The RichTextBox class documentation and the Document property should get you started at modifying the underlying structure of the document after loading or during editing.

Edit individual character or word in RichEditBox?

Basically in RichEditBox you can paste text and it can automatically format that pasted text according to what you copied. I am wondering how to set a specific part of the document, such as a word, or sentence, to a specific form of font colour, or font style, or any other properties regarding text.
I cannot find any real documentation on how to do such a thing.
EDIT:
I found:
richEditBox.Document.GetRange(3, 10).CharacterFormat.ForegroundColor = Windows.UI.Colors.Blue;
If someone knows something nicer, post please.
Simply use
richEditBox.Document.GetRange(3, 10).CharacterFormat.ForegroundColor = Windows.UI.Colors.Blue;
Are you looking for something like this?
http://msdn.microsoft.com/en-us/library/0dhsw8eb(v=vs.90).aspx

Multilingual text copy/paste to Textbox in winforms?

I have textbox which accepts string for File name. The user can give/type any name for textbox, even he copy text (Multilingual text) and paste in the textbox. He can copy from any source.
The issue is, how do i detect the 'Language' of copied text which is pasted in textbox and
display the text in that corresponding language?
There is a .net library that does the job:ntextcat ... and ... it`s open source.
Also, you can use google-api-for-dotnet to the actual translate.
May be ill think of having a common words in a dictionary for each language and looking it up when user pastes it. If i find it in any of these dictionary item, then ill know its language type. May not be the best solution your looking for. Open to experts comment here.

change font of a string in C#

I am reading a txt file and writing it's content to another txt file.
Before writing the content to new file i have to change the font of the string (string that is read from another file).
How should i do this.
Please help.
Strings don't inherently have fonts. If the string represents RTF or something like that, then that's a different matter - it's effectively changing the font within the RTF format.
However, if you're just writing a plain text file, it's entirely up to the display client which font it uses.
Sorry!! friend. you can't change the fonts or size of string. C# don't know where your are going to write the string text... Console or any text file or label...
Just think what happens if it supports, lets u set some format to string and in place of writing to a file you are printing to console (Console is not rich in fonts)
anyway !! but if you want to do it, it depends on the type of file you are writting. notepad, or rtf orword document. There you can find changing fonts very easily.
If it is pure text file, then you cannot!
If you are writing a Word Doc, using some Office API then you can!
a simple string has no font. A font is used for printing or displaying a string, but a string itself is only text. If you are talking about some encoded text (like rtf) you are reading, you will need to parse the text into something interpretable and go on from there.
If you only want to put out one or more strings in a textbox using different fonts, you will need to use a control that supports formatting.

c# Write data to an rtf file

I am trying to write data to an RTF file and set certain characters to a different color. I found out how to do it in a box but I can't figure out how to write it to a file.
If by "do it on a box" you mean RichTextBox you can use the RichTextBox.SaveFile() mothod.
Take a look here.

Categories

Resources