如何将数据从richtextbox传输到另一个richtextbox WPF C#

时间:2012-10-08 18:37:39

标签: c# wpf

我在将我的richtextbox中的数据显示或传输到其他richtextbox时遇到问题......

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
}); 

2 个答案:

答案 0 :(得分:4)

使用Text的{​​{1}}属性代替TextRange

.ToString()的内容作为字符串获取的方法:

RichTextBox

public static string GetStringFromRichTextBox(RichTextBox richTextBox) { TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); return textRange.Text; } 的内容作为富文本格式的方法:

RichTextBox

编辑:您可以通过执行以下操作,将GetRtfStringFromRichTextBox()返回的富文本放入另一个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()); } 控件中:

RichText

答案 1 :(得分:0)

不会将 RichTextBox 的内容作为字符串获取,只是类似于 以下(在 VB.Net 中)

Dim strText as string = MyRTB.text