我正在使用的文本编辑器在保存为RTF格式时可以正确保存其内容。但是,如果我保存为(纯文本)txt,html,dvc和其他基于(纯文本)txt的格式,则所有段落都消失了。例如:
这是第一段
这是第二段
这是第三段
成为:
这是第一段这是第二段这是第三段
如果我使用SendKeys和System.Diagnostics.Process.Start将RichTextBox内容发送到记事本,则会保留段落。 因此,问题似乎出在“保存/另存为菜单”代码中。我正在添加“另存为”菜单的代码。
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");
}
}