【问题标题】:Change color of all instances of a Word(s) in RichTextBox更改 RichTextBox 中所有单词实例的颜色
【发布时间】:2013-02-08 17:02:39
【问题描述】:

我试图做到这一点,我可以在代码中指定一个或多个单词,并且当我编译/调试时,我希望程序在 Richtextbox 中搜索这些单词的所有实例并更改那里的颜色。

Dim GRAB as String = New WebClient().DownloadString("example.com")
RichTextBox1.Text = GRAB
` Color Word Code Here

我在 google 上查了很多东西,但我尝试过的所有东西都只会突出显示第一个单词。

对不起,如果我打字不好,我打字时胳膊骨折了..

谁能帮我解决这个问题,或者写一个快速的sn-p?

【问题讨论】:

标签: vb.net colors richtextbox highlight


【解决方案1】:

您需要选择会改变颜色的文本。

RichTextBox1.Select(RichTextBox1.Text.IndexOf("example"),4)
 RichTextBox1.SelectionColor = Color.Red

会将“.com”渲染为红色 或

RichTextBox1.Select(6,4)
  RichTextBox1.SelectionColor = Color.Red

也会这样做

【讨论】:

  • 哎呀,我需要放慢速度阅读整个问题!仅当字符串 GRAB 为“example.com”时,答案才成立。
【解决方案2】:

试试这个:

Dim wordslist As New List(Of String)
wordslist.Add("Hello")
wordslist.Add("World")

Dim len As Integer = RichTextBox1.TextLength

For Each word As String In wordslist

    Dim lastindex = RichTextBox1.Text.LastIndexOf(word)
    Dim index As Integer = 0

    While index < lastindex

    RichTextBox1.Find(word, index, len, RichTextBoxFinds.None)
    RichTextBox1.SelectionColor = Color.Blue
    index = RichTextBox1.Text.IndexOf(word, index) + 1

    End While

Next

Here的C#修改和翻译

【讨论】:

    【解决方案3】:

    假设你想把'Dim'变成蓝色作为VS:-

    粘贴这个:

    If RichTextBox1.Text.EndsWith("Dim") Then
            RichTextBox1.Select(RichTextBox1.TextLength - 3, 3)
            RichTextBox1.SelectionColor = Color.Blue
            RichTextBox1.Select(RichTextBox1.TextLength, RichTextBox1.TextLength)
            RichTextBox1.SelectionColor = Color.Black
        End If
    

    将此代码添加到 RichTextBox 到 Text_Changed。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2010-11-06
      • 2016-09-01
      • 1970-01-01
      相关资源
      最近更新 更多