0

The text editor I am working on properly saves its contents when saving to RTF format. However if I save to (plain)txt, html, dvc and other (plain)txt based formats all paragraphs are gone. For example:

This is the first paragraph

This is the second paragraph

This is the third paragraph

Becomes:

This is the first paragraphThis is the second paragraphThis is the third paragraph

If I use SendKeys and System.Diagnostics.Process.Start to send the RichTextBox contents to Notepad, paragraphs are maintained. So the problem appears to be in the Save/Save as Menu code. I'm adding the code of the Save As menu.

 private void SaveAsToolStripMenuItem_Click(object sender, System.EventArgs e)
    {

        try
        {
            SaveFileDialog1.Title = "RTE - Save File";
            SaveFileDialog1.DefaultExt = "rtf";
            SaveFileDialog1.Filter = "Rich Text Files|*.rtf|Text Files|*.txt|XML Files|*.xml|INI Files|*.ini|DVC Files|*.dvc|CSV Files |*.csv |CS Files |*.cs |HTML Files|*.html|HTML Files|*.htm|All Files|*.*";
            SaveFileDialog1.FilterIndex = 1;

            if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                if (SaveFileDialog1.FileName == "")
                {
                    return;
                }

                string strExt;
                strExt = System.IO.Path.GetExtension(SaveFileDialog1.FileName);
                strExt = strExt.ToUpper();

                if (strExt == ".RTF")
                {
                    rtbDoc.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText);
                }
                else
                {
                    System.IO.StreamWriter txtWriter;
                    txtWriter = new System.IO.StreamWriter(SaveFileDialog1.FileName);
                    txtWriter.Write(rtbDoc.Text);
                    txtWriter.Close();
                    txtWriter = null;
                    rtbDoc.SelectionStart = 0;
                    rtbDoc.SelectionLength = 0;
                }

                currentFile = SaveFileDialog1.FileName;
                rtbDoc.Modified = false;
                this.Text = "Editor: " + currentFile.ToString();
                MessageBox.Show(currentFile.ToString() + " saved.", "File Save");
            }
            else
            {

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }
    }

    private void ExitToolStripMenuItem_Click(object sender, System.EventArgs e)
    {
        try
        {
            if (rtbDoc.Modified == true)
            {
                System.Windows.Forms.DialogResult answer;
                answer = MessageBox.Show("Save file?", "Document not saved", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (answer == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }

                else if (answer == System.Windows.Forms.DialogResult.Yes)
                {
                    return;
                }
                else
                {
                    rtbDoc.Modified = false;
                    Application.Exit();
                }
            }
            else
            {
                rtbDoc.Modified = false;
                Application.Exit();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }
    }
  • RTF stores not in plain text, it had special tags to preserve the formatting, while txt dont have such feature. it only saves text.. try to open RTF in notepad.. – Bagus Tesa Jan 21 '19 at 04:06
  • You are right but still, saving as plaintext should maintain paragraphs. Creating any kind of document in notepad for instance and then saving it won't remove the paragraphs. – Interestednewbie Jan 21 '19 at 18:23
  • i believe the issue lies on `txtWriter.Write(rtbDoc.Text);` as i think it pops out a plain text without new line characters, while the RTF output had a bunch of tags to define the paragraph. you might want to output RTF first, then convert it to plain text. perhaps [this QA on converting RTF to plain text](https://stackoverflow.com/a/29922772/4648586) may help. – Bagus Tesa Jan 22 '19 at 00:49
  • 1
    Thank you! You got me on the right track. This turned out simpler than I thought. You are right I got rid of txtWriter entirely and only had to add this in the "else" section: RichTextBoxStreamType.PlainText); – Interestednewbie Jan 22 '19 at 19:41

0 Answers0