存储有关 richtextbox 文本的所有信息并重建它 - c# winforms
Store all information about the text of a richtextbox and reconstruct it- c# winforms
有没有办法将有关文本的所有信息存储在 richtextbox
(颜色、大小、字体等)中,并在另一个 richtextbox
、哪个表格或项目不一样?
例如,我有一个richtextbox,它的文本包含多种字体和颜色,并且有些行居中,我想在另一个richtextbox中重建它。
我补充说新的richtextbox不在同一个项目中,所以我需要在某个地方恢复信息(例如,即使在字符串或文件中)。
要将文本和格式从一个 richTextBox 复制到另一个,只需使用:
richtextBox2.Rtf = richtextBox1.Rtf;
Rtf 属性 只是一个字符串,所以你可以用它来做任何你能用字符串做的事。
您可以按照以下步骤完成
假设我们有两个项目
第一个是 WinFormApp1 第二个是 WinFormApp2
将WinFormApp1中RichTextBox1
的RTF
保存到文本文件
在WinFormApp1中
const string path = @"D:\RichTextBox\Example.txt";
var rtbInfo = richTextBox1.Rtf;
if (!File.Exists(path))
{
File.Create(path);
TextWriter textWriter = new StreamWriter(path);
textWriter.WriteLine(rtbInfo);
textWriter.Close();
}
else if (File.Exists(path))
{
File.WriteAllText(path, rtbInfo);
}
从文本文件中读取数据赋值给WinFormApp2
中的RichTextBox1
的RTF
在 WinFormApp2
private void Form1_Load(object sender, EventArgs e)
{
const string path = @"D:\RichTextBox\Example.txt";
if (File.Exists(path))
{
richTextBox1.Rtf = System.IO.File.ReadAllText(path);
}
}
有没有办法将有关文本的所有信息存储在 richtextbox
(颜色、大小、字体等)中,并在另一个 richtextbox
、哪个表格或项目不一样?
例如,我有一个richtextbox,它的文本包含多种字体和颜色,并且有些行居中,我想在另一个richtextbox中重建它。
我补充说新的richtextbox不在同一个项目中,所以我需要在某个地方恢复信息(例如,即使在字符串或文件中)。
要将文本和格式从一个 richTextBox 复制到另一个,只需使用:
richtextBox2.Rtf = richtextBox1.Rtf;
Rtf 属性 只是一个字符串,所以你可以用它来做任何你能用字符串做的事。
您可以按照以下步骤完成
假设我们有两个项目
第一个是 WinFormApp1 第二个是 WinFormApp2
将WinFormApp1中
RichTextBox1
的RTF
保存到文本文件在WinFormApp1中
const string path = @"D:\RichTextBox\Example.txt"; var rtbInfo = richTextBox1.Rtf; if (!File.Exists(path)) { File.Create(path); TextWriter textWriter = new StreamWriter(path); textWriter.WriteLine(rtbInfo); textWriter.Close(); } else if (File.Exists(path)) { File.WriteAllText(path, rtbInfo); }
从文本文件中读取数据赋值给WinFormApp2
中的RichTextBox1
的RTF
在 WinFormApp2
private void Form1_Load(object sender, EventArgs e) { const string path = @"D:\RichTextBox\Example.txt"; if (File.Exists(path)) { richTextBox1.Rtf = System.IO.File.ReadAllText(path); } }