【发布时间】:2019-11-18 03:37:13
【问题描述】:
我希望能够在 RichTextBox 中突出显示(选择)文本而不影响当前的 .SelectionBackColor 或 .SelectionColor
我有一个包含大量文本的 RichTextBox。
我有一个 ListView,其中包含 ListView 中具有不同 .BackColor 和 .ForeColor 的字符串/单词列表。
我遍历 ListView 项目并使用 .SelectionBackColor 和 .SelectionColor 将文本颜色设置为与 ListView 中的 .BackColor 和 .ForeColor 相匹配,突出显示 RichTextBox 中的相应文本。
For Each verItem As ListViewItem In lvVer.Items
startindex = 0
While startindex < rtbMain.TextLength
Dim wordStartIndex As Integer = rtbMain.Find(verItem.Text, startindex, RichTextBoxFinds.None)
If wordStartIndex <> -1 Then
rtbMain.SelectionStart = wordStartIndex
rtbMain.SelectionLength = verItem.Text.Length
rtbMain.SelectionBackColor = verItem.BackColor
rtbMain.SelectionColor = verItem.ForeColor
Else
Exit While
End If
startindex += wordStartIndex + verItem.Text.Length
End While
Next
这一切都完美无缺,但我希望能够在列表视图中选择一个项目,然后使用标准突出显示(蓝色背景,白色文本)颜色突出显示 RichTextBox 中匹配的文本对象,同时仍保留下面的原始颜色.
如果我使用鼠标突出显示 RichTextBox 中的文本,它会以通常的 Windows 方式(蓝色背景,白色文本)临时突出显示。如果我随后在 RichTextBox 的其他位置单击鼠标,我在字符串上设置的原始颜色仍然存在。我想在代码中复制这种行为。
如果通过代码rtbMain.Select(wordStartIndex, verItem.Text.Length)“选择”文本,它不会被突出显示。它甚至也没有在视觉上被选中。显然我可以设置一个新的 .SelectionBackColor 和 .SelectionColor 但是我会失去我原来的颜色。
有没有办法以编程方式将鼠标滚动到 RichTextBox 文本上,并在不影响底层原始颜色的情况下选择并突出显示文本?
【问题讨论】:
标签: richtextbox highlight