在 RichTextBox 中缩进 RTF 文本而不丢失 RTF 样式

Indent RTF Text in RichTextBox without losing the RTF style

我想在不丢失 RTF 样式的情况下缩进 RichTextBox 中的 RTF 文本。

Dim Alinea As String = "    "

Private Sub Indent_Click(sender As Object, e As EventArgs) Handles Indent.Click
    Try
        Dim Output As String = Nothing
        Dim Split() As String = RichTextBox1.Lines
        For i = 0 To Split.Length - 1
            Output = String.Concat(Output, Split(i).Insert(0, Alinea), If(Not i = Split.Length - 1, vbNewLine, Nothing))
        Next
        RichTextBox1.Text = Output
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

前面的代码有效,但是 returns 文本没有任何样式。

我只想在 RichTextBox 文本的所有行首添加 Alinea。 我尝试使用 RichTextBox1.Rtf 属性,但它显示一个 MsgBox 说 "File format not valid".

不使用 RichTextBox1.Lines,而是使用 RichTextBox1.Rtf。

RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(vbCrLf, vbCrLf & vbTab)

这行得通,但您可能需要键入 \par\par & vbcrlf 之类的内容以更符合 rtf 标准。

RichTextBox1.Rtf = RichTextBox1.Rtf.Replace("\par" & vbCrLf, "\par" & vbCrLf & vbTab)

"It is left as an exercise to the reader" 使其在第一行和“\par”之后的任何空白字符上工作。 (我一直讨厌这句话。)