I write some codes for simple text editor in C# and I use rich text box control, I found a problem that I can’t solve. The problem is when I save a file in my text editor and then try to reopen it using windows notepad, it become in one line, this is the example
This is when I write and save from my app

After I save it and open in windows notepad it becomes like this

here are my code for saving a fie
try
{
saveFileDialog1.ShowDialog();
this.Text = file = toolStripTextBox1.Text = saveFileDialog1.FileName;
isi = richTextBox1.Text;
write = new System.IO.StreamWriter(file);
write.WriteLine(isi);
write.Close();
toolStripStatusLabel2.Text = "Saved";
}
catch (Exception)
{
toolStripStatusLabel2.Text = "Save cancelled by user";
}
do you have any idea how to fix it?
You are probably getting this because you are trying to save
richTextBox1.Text(the whole text) in one line only using the following codeIt’s recommended to use
write.WriteLine()on a specific line number inrichTextBox1then move to another line.Example
Another Solution
There’s already a built-in function for
RichTextBoxto save a file with a specific encoding. You may useRichTextBox.SaveFile()for this purpose.Example
Where path represents
saveFileDialog1.FileNamein your code. ForRichTextBoxStreamType, it’s best to set it asRichTextBoxStreamType.PlainTextas long as you do not use RTF such as Color/Font/Protection/Indent/etc…Then, you may read the file again using the following method
NOTICE: If the file is not in RTF and you try to read it in RTF (
RichTextBox.LoadFile(string path, RichTextBoxStreamType.RichText);)you may encounter formatting errors. In this case, you’ll need to catch the exception and read the file in a Plain or Unicode encoding.
Example
Thanks,
I hope you find this helpful 🙂