Add simple formatting to the first line of RichTextBox

S-Soft 666 Reputation points
2023-01-18T01:38:46.1666667+00:00

Hello.

Using RichTextBox to convert a simple plain text string to rich text.

It does a great job as I don't need advanced formatting:

Dim runtimeRichTextBox As New RichTextBox

runtimeRichTextBox.Font = New Font(blah, 12)

runtimeRichTextBox.Text = "a multi line simple plain text string"

And get runtimeRichTextBox.Rtf as rtf

The only thing I need to to apply these 3 to the first line of my output rich text:

Make the 1st line center aligned + bold + red color.

Is that possible?

Thanks in advance

Developer technologies | VB
Developer technologies | Visual Studio | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-01-18T07:21:19.6866667+00:00

    Hi @S-Soft , You can try the following code.

            runtimeRichTextBox.Find(RichTextBox1.Lines(0))
            runtimeRichTextBox.SelectionColor = Color.Red
            runtimeRichTextBox.SelectionAlignment = HorizontalAlignment.Center
            runtimeRichTextBox.SelectionFont = New Font(
             runtimeRichTextBox.SelectionFont.FontFamily,
             runtimeRichTextBox.SelectionFont.Size,
             FontStyle.Bold
             )
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2023-01-18T04:24:12.68+00:00

    Hi

    This seems to do what you ask.

        With RTB
          .SelectionStart = 0
          .SelectionLength = .Lines(0).Length
          .SelectionAlignment = HorizontalAlignment.Center
          .SelectionFont = New Font(.SelectionFont.FontFamily, .SelectionFont.SizeInPoints, FontStyle.Bold)
          .SelectionColor = Color.Red
        End With
    
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.