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");
}
}