c# - display formatted data from database to richtextbox -


i made program can add formatted (change font,size , colour) data using richtextbox ms access database, there normal text box store topics loaded listbox when click on topic in listbox supposed display formatted text in richtextbox, displays plain text topic clicked formatted text displays how text 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 * items name = '" + listitem.text + "'";             oledbcommand cmd = new oledbcommand(query, conn);             oledbdatareader reader;             try             {                 conn.open();                 reader = cmd.executereader();                 // reads data , fills combo box , 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) richitem.rtf = sdetail; displays formatted text when topic selected plain text says format invalid, have use in 2 more places. there check can first check if text has rtf properties or other way display both plain , formatted text?

rich text seems starts {\rtf (i may wrong. seems fair assumption). check that, you'll able decide.

please see link convenient extension method this.

code msdn forum

/// <summary> /// returns dataformat string of text. /// </summary> /// <param name="text">text check.</param> /// <returns>value <see cref="dataformats"/> enumeration.</returns> public static string getdataformat(this string text) {     // first validate text     if (string.isnullorempty(text)) return system.windows.dataformats.text;      // return right data     if (text.startswith(@"{\rtf")) return system.windows.dataformats.rtf;      // return default     return system.windows.dataformats.text; } 

usage:

var format = mystring.getdataformat();  if (format == system.windows.dataformats.rtf) {      // process rtf text } else  {     // process plain text } 

Comments