【问题标题】:RichTextBox Highlighting富文本框突出显示
【发布时间】: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


【解决方案1】:

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

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

这应该适用于您正在尝试做的事情

【讨论】:

  • -1。没有Select 这样的重载可以选择字符串值。
  • 尝试当你改变你的背景色时,把textcolor改为白色,把背景色变成浅蓝色
猜你喜欢
  • 2012-06-26
  • 2013-01-23
  • 1970-01-01
  • 2013-01-05
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2012-09-08
相关资源
最近更新 更多