【问题标题】:RichTextBox RTF issueRichTextBox RTF 问题
【发布时间】:2016-01-27 03:57:06
【问题描述】:

我有一个 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

我希望我能够把我的问题说清楚。

【问题讨论】:

    标签: c# winforms richtextbox


    【解决方案1】:

    您是否考虑过从剪贴板 hack 中粘贴它?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多