Sydcul Sydcul - 11 months ago 66 Question

RichTextBox multi-line layout in VB.NET

If this has been asked before, sorry.

How can i set up different fonts in one RichTextBox using VB.NET? Because when i do this:

Dim String as String = "text" & vbCrLf & "more text"

Form.RichTextBox.Text = String
Form.RichTextBox.Select(String.IndexOf("Score: 5"), Len("Score: 5"))
Form.RichTextBox.SelectionFont = New Font(Presentatie.rtxtPresentatie.SelectionFont, FontStyle.Bold)

It will only make the "core: 5" part bold (tested this with selecting only, too, then it will only select the "core: 5" part).

Anyone help me? I need to get the answer very fast so please!

EDIT: Solved. Used this:

Form.RichTextBox.Select(String.IndexOf("Score: 5") - 1, Len("Score: 5"))

Thanks to codingbiz

Answer Source

I didn't post my comment as answer because I was worried of when IndexOf returns 0, then -1 would throw an exception. So here is the solution from microsoft

 Public Sub SelectMyString()

    ' Create a string to search for the word "fox".
    Dim searchString As String = "fox"

    ' Determine the starting location of the word "fox".
    Dim index As Integer = Form.RichTextBox.IndexOf(searchString)

    ' Determine if the word has been found and select it if it was. 
    If index != -1 Then
       'Select the string using the index and the length of the string.
       Form.RichTextBox.Select(index, searchString.Length)
    End If
 End Sub