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.
精彩评论