Garena Plus Beta Garena Plus Beta - 8 months ago 84 Question

How to Format text within bracket for RichTextBox?

I have a string like : [1] Happy New year [2] Happy Birthday [3] Happy Together
And I want to format the number 1,2,3 as Red Color. So I put the string to a RichTextBox and do the search and format like the code below:
(What I do is, find "[" and "]" and save to a Global variable (i,j), then everytime I get a new set of i,j the formating event fired. However, it does not do what I expected. : (

Public i, j As Integer
Dim s As String = "[1] Happy New year [2] Happy Birthday [3] Happy Together"

'Button Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim count As Integer = 0
rtfbuffer.Text = s
rtfbuffer.Font = New Font("Tahoma", 12, FontStyle.Bold)
rtfbuffer.ForeColor = Color.Black

Do While count < s.Length - 1

If s.Chars(count) = "[" Then
i = count
count += 1
ElseIf s.Chars(count) = "]" Then
j = count + 1

count += 1
rtfbuffer.Select(i, j)
rtfbuffer.SelectionColor = Color.Red

count += 1
End If


Finally, I could get only First text within the "[ ]" Red.. Look at the code, I could not understand why it does not go through the reamaining text. could you show me how to correct it?
Thank you very much ~


The second argument to the Select method is the length of the text to select. See MSDN.

Public Sub Select ( start As Integer, length As Integer )


start Type: System.Int32 The position of the first character in the current text selection within the text box.

length Type: System.Int32 The number of characters to select.

So, in your code, when you call Select you can calculate the length to select (and then highlight) by providing j - i instead of just j. It works for the first time in your code, because the 'end index' (j) is actually the same as the length i.e. 3.

rtfbuffer.Select(i, j - i)
rtfbuffer.SelectionColor = Color.Red