【问题标题】:Highlight specific words in a RichTextBox突出显示 RichTextBox 中的特定单词
【发布时间】:2014-03-08 23:12:48
【问题描述】:
我该怎么做:我按下一个按钮,然后它会找到每个出现的单词让我们说“测试”并将单词的前景色更改为浅绿色......
txtText.Find("dim")
txtText.SelectionColor = Color.Aqua
如果我这样做,它只会找到第一个出现的地方,让它变成水色,如果我再次按下它,它就会把所有的文本都变成这样。谁能帮帮我?
【问题讨论】:
标签:
richtextbox
vb.net-2010
highlighting
【解决方案1】:
您可以编写以下代码来做到这一点:
首先你需要按钮(FindBUT)和RichTextBox(TextBox)
Public Class Research
Private Sub FindBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindBUT.Click
'Check button text
If FindBUT.Text = ("Find ""dim""") Then
'Return all text color to black
TextBox.ForeColor = Color.Black
'Find "dim" word
TextBox.Find("dim")
TextBox.SelectionColor = Color.Aqua
'change button text to ("Find all")
FindBUT.Text = ("Find all")
'Check button text
ElseIf FindBUT.Text = ("Find all") Then
'change all text color to Aqua
TextBox.ForeColor = Color.Aqua
'change button text to ("Find ""dim"" ")
FindBUT.Text = ("Find ""dim""")
End If
End Sub
结束类