如何将数据从richtextbox传输到另一个richtextbox WPF C#
您好,我在显示我的richtextbox中的数据或将其传输到其他richtextbox时遇到问题如何将数据从richtextbox传输到另一个richtextbox WPF C#,c#,wpf,C#,Wpf,您好,我在显示我的richtextbox中的数据或将其传输到其他richtextbox时遇到问题 richtextbox1.Document = richtextbox2.Document; //This will be the idea.. 实际上,我计划做的是,我想把我的数据从我的数据库传输到我的listview,它将显示它是什么样子 SQLDataEHRemarks = myData["remarks"].ToString();// Here is my field from my da
richtextbox1.Document = richtextbox2.Document; //This will be the idea..
实际上,我计划做的是,我想把我的数据从我的数据库传输到我的listview,它将显示它是什么样子
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
现在我想做的是,我将把这些数据加载到我的ListView中。。或其他控件,如textblock或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
});
使用
textrage
的Text
属性,而不是.ToString()
将RichTextBox
的内容获取为字符串的方法:
public static string GetStringFromRichTextBox(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
将RichTextBox
的内容获取为富文本的方法:
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());
}
编辑:通过执行以下操作,可以将从GetRtfStringFromRichTextBox()返回的富文本放入另一个RichText
控件中:
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;
把RichTextBox的内容作为字符串不是很简单吗 以下内容(在VB.Net中)
非常感谢你……:)顺便问一下,从listview或textbox发送到richtextbox的数据怎么样?很抱歉…@KenshiHemura检查我答案的最后一点,寻找一种设置RichTextBox内容的方法。将RichTextBox的内容设置为字符串难道不是简单地类似于:(在VB.Net中)Dim strText as string=MyRTB.text吗
Dim strText as string = MyRTB.text