【问题标题】:RichTextBox multi-line layout in VB.NETVB.NET 中的 RichTextBox 多行布局
【发布时间】:2012-12-16 18:29:48
【问题描述】:

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

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

【问题讨论】:

  • 旁注;命名你的变量String 不是最好的。似乎你的索引是一个 - 试试Form.RichTextBox.Select(myString.IndexOf("....") - 1)
  • 我知道字符串,它只是用于stackoverflow。我马上试试这个。
  • 成功了!谢谢。有点奇怪,如果我用其他文本尝试这个,当我使用 - 1 时它会起作用并抛出错误,可能与空间有关。很奇怪。
  • @codingbiz:看起来您找到了 OP 正在寻找的解决方案。请张贴作为答案以被接受。

标签: vb.net richtextbox


【解决方案1】:

我没有发布我的评论作为答案,因为我担心 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

【讨论】:

  • 好的,很好,但是如果有人在他的标签中有 vb.net,请不要发布 C#。只是一个旁注。我明白了。
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多