【发布时间】:2014-02-09 17:27:00
【问题描述】:
目标
根据在 DataGridView 中选择的内容,以编程方式突出显示 RichTextBox 中包含的字符串的一部分。
查看屏幕截图以获得更直观的示例
可以看到,选择选项类型(Ec-Electrical)时,其选项将显示在右侧的另一个DataGridView中。从那里,用户可以检查他希望包含在传送带功能摘要中的那些(在本例中为 Photoeye)并突出显示。
使用的代码
每次Option类型DataGridView有selectionchanged事件时,都会执行该方法。
Private Sub SummaryOptionsHighlight()
Dim strToFind As String = ""
Dim textEnd As Integer = rtbSummary.TextLength
Dim index As Integer = 0
Dim lastIndex As Integer = 0
'Builds the string to find based on custom classes - works fine
For Each o As clsConveyorFunctionOptions In lst_Options
If o.Included Then
If strToFind.Length <> 0 Then strToFind += ", "
If o.Optn.IsMultipleQty And o.Qty > 0 Then
strToFind += o.Optn.Description & " (" & o.Qty & "x)"
Else
strToFind += o.Optn.Description
End If
End If
Next
'Retrieves the last index of the found string: ex. Photoeye (3x)
lastIndex = rtbSummary.Text.LastIndexOf(strToFind)
'Find and set the selection back color of the RichTextBox
While index < lastIndex
rtbSummary.Find(strToFind, index, textEnd, RichTextBoxFinds.None)
rtbSummary.SelectionBackColor = SystemColors.Highlight
index = rtbSummary.Text.IndexOf(strToFind, index) + 1
End While
End Sub
问题
所发生的不是突出显示,而是为该选择设置了更多的背景色。我之所以这么说是因为当我单击 RichTextBox 以指示它具有焦点时,它不会清除突出显示。也许有一个实际的highlight而不是背景颜色选择?
看看区别:
选择背景颜色:
突出显示:
【问题讨论】:
-
只需在您的 SelectionBackColor 属性后添加
rtbSummary.SelectionColor = SystemColors.HighlightText。焦点是一个不同的问题。目前还不清楚这些突出显示的项目会发生什么。 -
@LarsTech 对于我关注 RichTextBox 时突出显示的项目,它们应该恢复到原来的颜色。您的评论帮助我找到了解决方案。谢谢
标签: vb.net richtextbox highlight