RichTextBox突出显示

时间:2014-01-20 13:49:46

标签: vb.net richtextbox highlight

目标

根据在DataGridView中选择的内容,以编程方式突出显示 RichTextBox中包含的字符串的一部分。

查看截图以获取更直观的示例 Selected Option Type Highlights it's related options

如您所见,当选择选项类型(EC - Electrical)时,其选项将显示在右侧的另一个DataGridView中。从那里,用户可以检查他希望包含在传送带功能摘要中的那些(在这种情况下是Photoeye)并且它会突出显示。


使用的代码

每次Option Type 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来表明它有焦点时,它并没有清除突出显示。也许有一个实际的突出显示而不是背景颜色选择?

查看差异:

选择背面颜色:SelectionBackColor

突出显示:Manual Highlight

1 个答案:

答案 0 :(得分:-2)

要突出显示,您只需要这样做:

if richtextbox1.text.contians("photoeye") then
richtextbox1.select("photoeye")
end if

这应该适用于你想要做的事情

相关问题