how to transfer data from richtextbox to another richtextbox WPF C# - c#

hi i have a problem in displaying or transfering the data in my richtextbox to other richtextbox...
richtextbox1.Document = richtextbox2.Document; //This will be the idea..
actually what i'm planning to do is, i want to transfer my data from my database to my listview which will display as what it is
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 is, im going to load this data to my ListView.. or other 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
});

Use the Text property of the TextRange instead of .ToString()
Method to get the content of a RichTextBox as string:
public static string GetStringFromRichTextBox(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
Method to get the content of a RichTextBox 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 the 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;

Wouldn't getting the contents of a RichTextBox as string simply be something like
the following (in VB.Net)
Dim strText as string = MyRTB.text

Related

RIchTextBox Rtf encoding

I created a richtextbox in wpf where i can select text and link it to a file or resource.
Let's say i want to add text something into textbox and add hyperlink to D:\cdrréper
I attach a part of the code:
_link.CommandParameter = path;
_link.NavigateUri = new Uri(path, UriKind.Absolute);
_link.IsEnabled = true;
using (MemoryStream ms = new MemoryStream()) {
TextRange tr = new TextRange(_link.ContentStart, _link.ContentEnd);
tr.Save(ms, DataFormats.Rtf);
richTextBox.Selection.Load(ms, DataFormats.Rtf);
}
The current text value in richtext box after load is
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Arial;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green102\blue204;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ul\cf3\ltrch {\field{\*\fldinst { HYPERLINK "D:\\\\cdrr'e9per" }}{\fldrslt {something}}}}\li0\ri0\sa75\sb75\fi0\ql\par}
}
}
Hyperlink value from RTF is { HYPERLINK "D:\\cdrr'e9per" }.
When i'm trying to access the path is not working because cannot be founded. é character is not encoded correctly. Do you have any suggestions about how i can fix this? Thanks for any help.
In the Hyperlink section is needed to have a \ before 'e9. When we load the rtf string into a TextRange we need to Load it as Xaml.
tr.Load(ms, DataFormats.Xaml);

RTF string with image from WPF RichTextBox won't display in WinForms RichTextBox

The RTF string below contains an image and was created from a WPF RichTextBox. When I set the RTF string below to a WinForms RichTextBox.Rtf property the image is not displayed. How can I get the image to show in the WinForms RichTextBox?
Here is the code to create the RTF string from the WPF RichTextBox:
private static string ExportRtf(RichTextBox richTextBox)
{
var document = richTextBox.Document;
var r = new TextRange(document.ContentStart, document.ContentEnd);
using (var stream = new MemoryStream())
{
r.Save(stream, DataFormats.Rtf);
return encoding.GetString(stream.ToArray());
}
}
Here is the WPF RichTextBox display:
Here is the WinForms RichTextBox display:
Here is the RTF string:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Calibri;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs21\f2\cf0 \cf0\ql{\f2 {\ltrch help}{\ltrch {\*\shppict{\pict\picwgoal435\pichgoal300\pngblip
424d460900000000000036000000280000001d00000014000000010020000000000000000000c40e0000c40e00000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3afff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe4a421ffe4a421ffe4a421ffe4a421ffe4a421ffffffffffffffffff358f3afff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe4a421ffe4a421fff8f8f8ffe4a421ffe4a421ffffffffffffffffff358f3afff8f8f8fff8f8f8ff9e9e9fff444444ff444444ffbfbfc0fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe4a421ffe4a421fff8f8f8ffe4a421ffe4a421ffffffffffffffffff358f3afff8f8f8ff9e9e9fff444444ffedededfff8f8f8ffe1e1e2fff8f8f8ff444444fff8f8f8ff444444fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe4a421ffe4a421ffe4a421ffe4a421ffe4a421ffffffffffffffffff358f3afff8f8f8ff444444ffcbcbccfff8f8f8fff8f8f8fff8f8f8ff444444ff444444ff444444ff444444ff444444fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3afff8f8f8ff444444ffcbcbccfff8f8f8fff8f8f8fff8f8f8fff8f8f8ff444444fff8f8f8ff444444fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe6b145ffeed49efff9f7f2ffefd6a4ffe5ad39fffdfefdffffffffff358f3afff8f8f8ffb4b4b5ff444444ffedededfff8f8f8ffe1e1e2ff444444ff444444ff444444ff444444ff444444fff8f8f8ff358f3afffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5ead5ffe7b44cffe3a525ffe6b248fff3e7cdffffffffffffffffff358f3afff8f8f8fff8f8f8ffb4b4b5ff444444ff444444ffb4b4b5fff8f8f8ff444444fff8f8f8ff444444fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3afff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3afff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8fff8f8f8ff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3aff358f3affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff}}}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}

extract lines from a flowdocument in a richtextbox starting with specific words WPF

I'm very new to wpf and I would like to make a text analysing tool.
I already know how to import text into rich textbox and format it properly, but now I want to run a method that extracts all lines in the flowdocument that start with INT or EXT and place them in a listbox. It seems to be quite easier to do this in winforms than in WPF.
Is there someone who can guide me with this?
I wish I could already provide some code but the flowdocument is new to me as is wpf.
I have written a code snippet to collect the lines that begin with INT or EXT.
I am sure the code is not optimal, because i am not practised with RichTextBox, but i think it is very easy to understand.
private List<string> CollectLines()
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
TestRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
TestRichTextBox.Document.ContentEnd);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
var text = textRange.Text;
List<string> resultList = new List<string>();
// Collect all line that begin with INT or EXT
// Or use .Contains if the line could begin with a tab (\t), spacing or whatever
using (StringReader sr = new StringReader(text))
{
var line = sr.ReadLine();
while (line != null)
{
if (line.StartsWith("INT") || line.StartsWith("EXT"))
{
resultList.Add(line);
}
line = sr.ReadLine();
}
}
return resultList;
}
Maybe you can find out how you can put the list into a listbox yourself :)

richtext to byte[] and byte[] to richtext in WPF [duplicate]

How do I get the text in RTF of a RichTextBox? I'm trying to get like this, but the property does not exist.
RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
To get the actual XAML created by the user inside of the RichTextBox:
TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
tr.Save(ms, DataFormats.Xaml);
string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
EDIT: I don't have code in front of me to test, but an instance of the TextRange type has a Save (to stream) method that takes a DataFormats parameter, which can be DataFormats.Rtf
There are 2 RichTextBox classes, one from the winforms framework and one from the WPF framework:
System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;
Only the Winforms RichTextBox has an Rtf property, the other has a Document property which contains a FlowDocument.

Get path geometry from FlowDocument object

Can someone tell me how to get path geometry from a WPF FlowDocument object? Please note that I do not want to use FormattedText. Thanks.
A FlowDocument can be viewed in any number of ways, but a Path is a fixed shape. I think maybe you really want some simplified, visual-only form of a FlowDocument's contents.
In that case you might try converting the FlowDocument to an XPS FixedDocument - the FixedPages have Canvases containing a bunch of Paths and Glyphs.
Get the Text property of a TextRange object initialized over the entire FlowDocument:
FlowDocument myFlowDocument = new FlowDocument(); //get your FlowDocument
//put in some (or it already has) text
string inText = "Hello, WPF World!";
TextRange tr = new TextRange(FlowDocument.ContentStart, FlowDocument.ContentEnd);
tr.Text = inText;
//get the current text out of the FlowDocument
TextRange trPrime = new TextRange(FlowDocument.ContentStart, FlowDocument.ContentEnd);
string outText = trPrime.Text;
//now outText == "Hello, WPF World!";
//to get formatting, looks like you would use myFlowDocument.TextEffects
Can you use
ChildVisual = VisualTreeHelper.GetChild(Visual yourVisual)
Dunno if you can take a Visual and turn it into a path geometry..

Categories

Resources