Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using richtextbox.appendtext to show a message in RichTextBox. Like when I click on some button, a "click on button" message will show in a rich text box.

I used the selection.color method, but the problem is that when I go to save this file in RTF format the blue color is gone while saving, and I want to save the file with the colour text that show in the rich text box.

What I have tried:

C#
private void frmMainPage_FormClosing(object sender, FormClosingEventArgs e)
           {
           if (firstStart == 0)
           {
               firstStart = 1;

               var confirmResult = MessageBox.Show("Do you want to quit the application?",
                                    "Quit?",
                                    MessageBoxButtons.YesNo);
              if (confirmResult == DialogResult.Yes )
               {

                   Log.Write(richTextBox1.Text += "Application Ended: " + "[" + sCounter() + "] " + DateTime.Now);

                   //if (t != null)
                   //{
                   //    t.Abort();
                   //}
                   // Close transaction connection
                   //TextRange range;
                   //FileStream stream;
                   //stream = new FileStream("myrichtextbox.txt", FileMode.Create);
                   // richTextBox1.SaveFile("C:\\MyDocument.rtf", RichTextBoxStreamType.RichText);

                   SaveFileDialog saveFile1 = new SaveFileDialog();

                   // Initialize the SaveFileDialog to specify the RTF extention for the file.
                   saveFile1.DefaultExt = "*.rtf";
                   saveFile1.Filter = "RTF Files|*.rtf";
                   if (richTextBox1.Text == String.Empty)
                   {
                       Application.ExitThread();
                   }

                   // Determine whether the user selected a file name from the saveFileDialog.

                   else if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                      saveFile1.FileName.Length > 0  )
                   {
                       // Save the contents of the RichTextBox into the file.
                       if (button1WasClicked == false)
                       {

                           richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);

                       }

                   }

                       Application.ExitThread();

               }

               else
               {
                   e.Cancel = true;
               }
           }

       }

This is my closing and saving code.
Posted
Updated 24-Jan-22 6:11am
v2

1 solution

It works for me:
C#
private void FrmMain_Shown(object sender, EventArgs epp)
    {
    MyRichTextBox.Text = "Default:InRed:InBlue";
    MyRichTextBox.SelectionStart = MyRichTextBox.Text.IndexOf(":") + 1;
    MyRichTextBox.SelectionLength = 5;
    MyRichTextBox.SelectionColor = Color.Red;
    MyRichTextBox.SelectionStart = MyRichTextBox.Text.LastIndexOf(":") + 1;
    MyRichTextBox.SelectionLength = 6;
    MyRichTextBox.SelectionColor = Color.Blue;
    }

private void MyButton_Click(object sender, EventArgs e)
    {
    string path = @"D:\Temp\AAARTF.rtf";
    MyRichTextBox.SaveFile(path, RichTextBoxStreamType.RichText);
    }

private void MyOtherButton_Click(object sender, EventArgs e)
    {
    string path = @"D:\Temp\AAARTF.rtf";
    MyRichTextBox.LoadFile(path);
    }
The file content is RTF, and it loads fine:
Rich Text Format
{\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue255;}
{\*\generator Riched20 10.0.22000}\viewkind4\uc1 
\pard\f0\fs17\lang2057 Default:\cf1 InRed\cf0 :\cf2 InBlue\cf0\par
}

So what am I doing that you aren't, or you doing that I am not?
 
Share this answer
 
v2

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