Drarig29
Drarig29

Reputation: 2245

Indent RTF Text in RichTextBox without losing the RTF style

I'd want to indent RTF text in a RichTextBox without losing the RTF style.

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

The previous code works, but it returns the text without any style.

I'd just like to add Alinea on all beginnings of line of the RichTextBox text. I've tried to use the RichTextBox1.Rtf property, but it shows a MsgBox saying "File format not valid".

Upvotes: 1

Views: 1405

Answers (1)

xpda
xpda

Reputation: 15813

Instead of using RichTextBox1.Lines, use RichTextBox1.Rtf.

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

This works, but you may want to key on something like \par or \par & vbcrlf to adhere more to the rtf standard.

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

"It is left as an exercise to the reader" to make it work on the first line and for any whitespace character following "\par". (I always hated that phrase.)

Upvotes: 1

Related Questions