2
Answers

C# how to display formatted text in richtextbod

Roelien Joubert

Roelien Joubert

11y
1.3k
1

Hi,

I made a program that can add formatted (change font,size and colour) data using a richtextbox into my MS Access Database,  there is also a normal text box to store the topics which is loaded to a listbox when you click on a topic in the listbox it is supposed to display the formatted text in another richtextbox, it displays the plain text perfectly but as soon  as a topic is clicked with formatted text it displays how the text was formatted:

 

 {\rtf\ansi\ansicpg 1252\deflang7177{\f0\fnil\fcharset 0 Microsoft Sans serif;}}

{\colortbl;\red0\green255\blue128;}

\viewkind4\uc 1\pard\cf1\fs17 now\cf0\par

}

 

My code:

private void listItem_SelectedIndexChanged(object sender, EventArgs e)

        {

            string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Temp\SumWizz.accdb";

            OleDbConnection conn = new OleDbConnection(connstring);

            string query = "SELECT * FROM Items WHERE Name = '" + listItem.Text + "'";

            OleDbCommand cmd = new OleDbCommand(query, conn);

            OleDbDataReader reader;

            try

            {

                conn.Open();

                reader = cmd.ExecuteReader();

                // reads the data and fills the combo box and listbox

 

                while (reader.Read())

                {

 

                  

                    string Sdetail = reader.GetString(2);

                    richItem.Text = Sdetail;

                 

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            conn.Close();

 

        }

 I have changed richItem (my richtextbox) to richItem.rtf = Sdetail;

Then it displays the formatted text perfectly but when topic selected with plain text it says format invalid, I have to use it in 2 more places. is there a check I can do to first check if the text has rtf properties or any other way to get it to display both plain and formatted text?

 

Thanks in advanced

Answers (2)