我有一个格式设施如粗体,斜体,对齐等选项的RichTextBox在Win表单应用程序,右击RTB打开ContexttoolStripmenu与插入数据库中的客户地址选项,这插入了一个字符串"$[ClientAddress]"在RTB文本。单击保存按钮时,$[ClientAddress]将替换为数据库中的实际地址(格式为rtf,如下所示:
string rtfText = richTextBox.Rtf;
rtfText = rtfText.Replace("$[ClientAddress]", $address);这里的问题是,当数据库中的实际地址字符串(以$ClientAddress格式)替换时,在富文本框中对“rtf”所做的所有格式化/样式设置都会丢失。
我们如何将$ClientAddress上的样式(格式化)传递给取代$ClientAddress的文本。
如果将地址作为纯文本而不是rtf文本从数据库中传递,则格式将保留,但地址的不同行之间的换行符将丢失,并且地址将打印为一条直线,如下所示:
39 East Tamaki Road, Papatoetoe, Auckland, New Zealand instead of the correct way as below as originally entered :
39 East Tamaki Road
Papatoetoe
Auckland
New Zealand我希望我已经能够把我的问题说清楚了。
发布于 2016-01-28 08:22:58
你有没有考虑过从剪贴板上粘贴出来的老方法?
下面是一个带有richtextbox (rtb1)和一个按钮(button1)的winform的概念验证。对不起,是用VB写的.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' This Richtextbox just to create the text that's in the database.
Dim rtb2 As New RichTextBox
rtb2.Text = "39 East Tamaki Road" & vbCrLf & "Papatoetoe" & vbCrLf & "Auckland, New Zealand"
rtb2.SelectAll()
rtb2.SelectionFont = New Font("Courier", 20)
rtb2.SelectionColor = Color.Blue
' This would be Clipboard.SetData(TextDataFormat.Rtf, $address)
Clipboard.SetData(TextDataFormat.Rtf, rtb2.SelectedRtf)
rtb2.DeselectAll()
rtb2.Clear()
' This is adding text to an existing richtextbox and then pasting in the replacement text.
Dim sPlaceHolder As String = "$[ClientAddress]"
rtb1.Clear()
rtb1.Text = "This is some random text plus the whole " & vbCrLf & sPlaceHolder & vbCrLf & "Place holder above."
rtb1.SelectAll()
rtb1.SelectionFont = New Font("Arial", 10)
rtb1.SelectionColor = Color.DarkRed
rtb1.DeselectAll()
Dim istart As Integer = rtb1.Find(sPlaceHolder)
Dim ilength As Integer = sPlaceHolder.Length
rtb1.SelectionStart = istart
rtb1.SelectionLength = ilength
rtb1.SelectedRtf = Clipboard.GetData(TextDataFormat.Rtf)
End Sub
End Classhttps://stackoverflow.com/questions/35028615
复制相似问题