Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently I am saving the contents of my rich textbox as so:

C#
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:

C#
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?
Posted
Updated 16-Aug-11 16:26pm
v3
Comments
Sergey Alexandrovich Kryukov 16-Aug-11 21:58pm    
RichTextBox in ASP.NET?! What is that? And OpenFileDialog? APS.NET, no kidding?
--SA

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

[EDIT] 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
 
Share this answer
 
v2
Comments
vlad781 17-Aug-11 2:04am    
I am not at my computer at the moment, so I can only guess what the code is.
And my guess is to place this into the save part of the save file dialog:

telep.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
telep.SaveFile(saveFile1.FileName, System.Windows.Forms.RichTextBox.BackColor);

That is most likely incorrect, but a heads up would be nice.
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:

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


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

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


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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900