![](/img/trans.png)
[英]How to append text to the richTextBox from another class in C# Winform?
[英]how to transfer data from richtextbox to another richtextbox WPF C#
嗨,我在將富文本框中的數據顯示或傳輸到其他富文本框時遇到問題...
richtextbox1.Document = richtextbox2.Document; //This will be the idea..
實際上我打算做的是,我想將我的數據從我的數據庫傳輸到我的列表視圖,它將顯示為
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 .. 或其他控件,如文本塊或文本框...
_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
});
使用TextRange
的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 中)
Dim strText as string = MyRTB.text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.