繁体   English   中英

RichTextBox RTF问题

[英]RichTextBox RTF issue

我在Win窗体应用程序中有一个RichTextBox,它具有格式化功能,如粗体,斜体,对齐方式等选项,右键单击RTB会打开ContexttoolStripmenu,并带有从数据库插入客户端地址的选项,该选项会在RTB文本中插入字符串"$[ClientAddress]" 单击保存按钮后, $[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

我希望我能够弄清楚我的问题。

您是否考虑过将旧内容从剪贴板中删除?

下面是带有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 Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM