如果之前有人问过,抱歉。
如何使用VB.NET在一个RichTextBox中设置不同的字体?因为当我这样做时:
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)
它只会使“core:5”部分变为粗体(仅通过选择进行测试,然后它将仅选择“core:5”部分)。
有人帮助我吗?我需要非常快速地得到答案,所以请!
编辑:解决了。使用它:Form.RichTextBox.Select(String.IndexOf("Score: 5") - 1, Len("Score: 5"))
感谢codingbiz
答案 0 :(得分:2)
我没有发表评论作为回答,因为我担心当IndexOf返回0时,-1会抛出异常。所以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