Vb.net - Multicolor RichTextBox

I would like to make a line of text in my multicolor richtextbox. I tried various implementations provided on the Internet and read on SelectedText and other topics, but it seems that it cannot make it work as I would like.

That's what i still have

RichTextBox1.Text = "This is black "
RichTextBox1.SelectionFont = New Font("Microsoft Sans Serif", 8.25, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "[BOLD GREEN]"
RichTextBox1.Text = RichTextBox1.Text + " black again"

The colors I want are indicated as text. What happens: the whole line turns green, "[BOLD GREEN]" appears at the beginning of the text field instead of the built-in one.

I want him to read this: "it's black," like black. "[BOLD GREEN]" as green and "black again" as black.

+3
source share
1 answer

, . , , , Paint. ...

, . , . , , - , . , .

Text , . , AppendText method.

, , , , , , . , , .

:

' Insert first snippet of text, with default formatting
RichTextBox1.Text = "This is black "

' Move the insertion point to the end of the line
RichTextBox1.Select(RichTextBox1.TextLength, 0)

'Set the formatting and insert the second snippet of text
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.AppendText("[BOLD GREEN]")

' Revert the formatting back to the defaults, and add the third snippet of text
RichTextBox1.SelectionFont = RichTextBox1.Font
RichTextBox1.SelectionColor = RichTextBox1.ForeColor
RichTextBox1.AppendText(" black again")

:

    sample RichTextBox with formatted text

+5

All Articles