简体   繁体   中英

How can I save the background color of a rich textbox along with its other contents

I want to be able to save the contents of a rich text box along with the color of the background all into an RTF file. I am currently using the save dialog method:

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);
    }
}

Is there anything I can add to acomidate my needs? Regards

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();

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.

it depends if the background color was assigned to the control or to the text. In the first case no way so you should make sure instead of setting such color to the control property you assign it to the text itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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