gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:24:46 27 4
gpt4 key购买 nike

您好,我在将 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.. 或其他控件,如文本 block 或文本框...

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

最佳答案

使用 TextRangeText 属性代替 .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;

关于c# - 如何将数据从 richtextbox 传输到另一个 richtextbox WPF C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com