How to transfer data from richtextbox to another richtextbox WPF C #

Hi, I have a problem while displaying or transferring data in my richtextbox to another richtextbox ...

richtextbox1.Document = richtextbox2.Document; //This will be the idea.. 

in fact, what I plan to do, I want to transfer my data from my database to my list, which will appear as what it

 SQLDataEHRemarks = myData["remarks"].ToString();// Here is my field from my database which is set as Memo RichTextBox NewRichtextBox = new RichTextBox();// Now i created a new Richtextbox for me to take the data from SQLDataEHRemarks... NewRichtextBox.Document.Blocks.Clear();// Clearing TextRange tr2 = new TextRange(NewRichtextBox.Document.ContentStart, NewRichtextBox.Document.ContentEnd);// I found this code from other forum and helps me a lot by loading data from the database.... MemoryStream ms2 = GetMemoryStreamFromString(SQLDataEHRemarks);//This will Convert to String tr2.Load(ms2, DataFormats.Rtf);//then Load the Data to my NewRichtextbox 

Now, what I want to do, I'm going to load this data into my ListView .. or another control like textblock or textbox ...

 _EmpHistoryDataCollection.Add(new EmployeeHistoryObject{ EHTrackNum = tr2.ToString() // The problem here is it will display only the first line of the paragraph.. not the whole paragraph }); 
+4
source share
1 answer

Use the Text property of the TextRange instead of .ToString()

A way to get the contents of a RichTextBox string as:

 public static string GetStringFromRichTextBox(RichTextBox richTextBox) { TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); return textRange.Text; } 

A way to get RichTextBox content as rich text:

 public static string GetRtfStringFromRichTextBox(RichTextBox richTextBox) { TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); MemoryStream ms = new MemoryStream(); textRange.Save(ms, DataFormats.Rtf); return Encoding.Default.GetString(ms.ToArray()); } 

Edit: You can put rich text returned from GetRtfStringFromRichTextBox () in another RichText control by doing the following:

 FlowDocument fd = new FlowDocument(); MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(richTextString)); TextRange textRange = new TextRange(fd.ContentStart, fd.ContentEnd); textRange.Load(ms, DataFormats.Rtf); richTextBox.Document = fd; 
+3
source

Source: https://habr.com/ru/post/1438477/


All Articles