如何在C#中保存富文本框的背景色 [英] How can I save the background color of a rich textbox in C#

查看:207
本文介绍了如何在C#中保存富文本框的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我将富文本框的内容保存为:

Currently I am saving the contents of my rich textbox as so:

private void asRTFToolStripMenuItem_Click(object sender, EventArgs e)
           {
               SaveFileDialog saveFile1 = new SaveFileDialog();
               saveFile1.DefaultExt = "*.rtf";
               saveFile1.Filter = "RTF Files|*.rtf|TXT Files|*.txt";
               if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                  saveFile1.FileName.Length > 0)
               {
                   telep.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
               }
           }


并将其加载为:


And loading it as:

private void rTFToolStripMenuItem_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFile1 = new OpenFileDialog();
                openFile1.DefaultExt = "*.rtf";
                openFile1.Filter = "RTF Files|*.rtf";
                if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                   openFile1.FileName.Length > 0)
                {
                    telep.LoadFile(openFile1.FileName, RichTextBoxStreamType.RichText);
                }
            }





How can I save and load the background color of my rich textbox?

推荐答案

错误的问题!请注意,不要腰部专家的时间. System.Windows.Forms.立即更改问题中的标签.

感谢您修复标签.因此,我的以下回答应该有效. [END EDIT]

现在,System.Windows.Forms.RichTextBox.BackColor怎么了?我知道这行得通吗?太难于查看有关该主题的标准MSDN页面了吗? http://msdn.microsoft.com/en-us/library/system. windows.forms.richtextbox.aspx [ ^ ].

—SA
Incorrect question! Please be careful, don''t waist expert''s time. It''s System.Windows.Forms. Change the tag in your question — immediately.

Thank you for fixing the tag. So, my answer below should work. [END EDIT]

Now, what wrong with System.Windows.Forms.RichTextBox.BackColor? I know it works? Too hard to look at standard MSDN page on the topic? http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx[^].

—SA


好,我想出了一种保存背景颜色的方法.这很糟糕,但是它确实做了.

在保存"按钮上单击,但在保存文件"对话框之前,请执行以下操作:

OK, I have figured a way to save the background color. It''s pretty bad, but it does what it does.

On the save button click but before the save file dialog, do this:

telep.SelectAll();
            telep.SelectionBackColor = telep.BackColor;
            telep.DeselectAll();



然后在加载按钮上单击,然后在加载文件对话框之后,执行以下操作:



then on the load button click, and after the load file dialog, do this:

telep.SelectAll();
            telep.BackColor = telep.SelectionBackColor;
            telep.DeselectAll();



所有这一切都是为了突出显示与富文本框相同颜色的文本,然后保存.并在加载后,将Rich文本框的背景色更改为突出显示的文本之一.



All this does is it highlights the text in the same color as the rich textbox then saves. And After loading, It changes the Rich text box back color to the one of the highlighted text.


问题是RichTextBox控件无法按MSDN页面中的状态运行.当调用RickTextBox控件的rtf方法时,将为您提供Richtextbox中所有文本和控件代码的字符串表示形式.但是,即使根据MSDN,也不包括背景色.因此,这是一个Microsoft错误.我自己碰到了这个问题.您可以保存RTF的每个属性(背景色除外).当使用rtf方法检索文本和控制代码时,这些控制代码会丢失.从我所看到的来看,还存在其他所有格式代码,只是没有背景色.我想不出解决此问题的方法.我正在使用VS2010,这个问题很容易重复.
The problem is that the RichTextBox control doesn''t function as it states in the MSDN page. When calling the rtf method of the RickTextBox control, you are provided with a string representation of all the text and control codes in the richtextbox. However, backcolor is not included, even though according to MSDN it is. Therefore, it is a microsoft bug. I just ran into this problem myself. You can save every attribute of the RTF except for the backcolor. Those control codees are missing when the rtf method is used to retrieve the text and control codes. From what I''ve seen, every other formatting code is present, just not backcolor. I cannot think of a work around for this problem. I''m using VS2010 and this problem is easily repeatable.


这篇关于如何在C#中保存富文本框的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆