简体   繁体   English

打开保存的文件,并对RTF文本进行更改

[英]Open the saved file with the changes made to the Rich text

I am new to C#. 我是C#的新手。 I am trying to build a notepad application. 我正在尝试构建一个记事本应用程序。 This how I am trying to write my file and save my file which is in a rich text. 这就是我尝试写入文件并保存格式文本的文件的方式。 But when I make the text bold and save it and try to open the saved file I can't see the bold text. 但是,当我将文本设为粗体并保存并尝试打开保存的文件时,我看不到粗体文本。 I only see the plain text that is inside the file. 我只看到文件内的纯文本。

What are the changes I should make in my code so that I can see the change I make in my rich text box? 我应该在代码中进行哪些更改,以便可以在富文本框中看到所做的更改?

var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    String filename = saveFileDialog1.FileName;
    if (filename != "")
    {
        File.WriteAllText(filename, "");
        StreamWriter strw = new StreamWriter(filename);
        strw.Write(_myRichTextBox.richTextBox1.Text);

The problem is that you don't save the formatting information. 问题是您不保存格式信息。 You save only the plain string content. 您只保存纯字符串内容。 The RichTextBox has tools to accomplish that. RichTextBox具有完成此任务的工具。

The simplest solution here would be to use the RichTextBox.SaveFile method. 这里最简单的解决方案是使用RichTextBox.SaveFile方法。 It will save it as *.RTF file and make sure that the format remains intact. 它将另存为*.RTF文件,并确保格式保持不变。

_myRichTextBox.SaveFile(filename);

to load the content back again use the RichTextBox.LoadFile method 使用RichTextBox.LoadFile方法再次加载内容

_myRichTextBox.LoadFile(filename);

You should save it as .rtf to save any format. 您应将其另存为.rtf以保存任何格式。

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                String filename = saveFileDialog1.FileName;
                if (filename != "")
                {
                    _myRichTextBox.SaveFile(filename);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM