Question

I am currently working on trying to save text from a richtextbox to a RTF file. I have it working so i can save the text to rtf file with colored text. However, I would like to preserve the background color of the box too in the document for viewing.

I am open to either saving the data into rtf or html as long as i can preserve all the color coded text, and display the appropriate back ground color in the document.

Below is the code i use to save as an RTF file with color coded text.

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFile1 = new SaveFileDialog();

    // Initialize the SaveFileDialog to specify the RTF extension for the file.
    saveFile1.DefaultExt = "*.rtf";
    saveFile1.Filter = "RTF Files|*.rtf";

    // Determine if the user selected a file name from the saveFileDialog. 
    if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFile1.FileName.Length > 0)
    {
        // Save the contents of the RichTextBox into the file.
        richtextbox.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
    }
}

Edit: I found out the following code can highlight the text with my background into an RTF file. For the moment this works, but I would still want to get opinions on better solutions.

Place below code before Savefile dialog open statement.

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

No correct solution

OTHER TIPS

My solution thus far has been to do the following before the Savefile dialog:

    richtextbox.SelectAll();
    richtextbox.SelectionBackColor = richtextbox.BackColor;
    richtextbox.DeselectAll();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top