0

如果以前有人问过这个问题,对不起。

如何使用 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

4

1 回答 1

2

我没有发布我的评论作为答案,因为我担心 IndexOf 返回 0 时,然后 -1 会引发异常。所以这是微软的解决方案

 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
于 2012-12-16T23:03:41.530 回答