突出显示文本框中的匹配集

Highlight sets of matches within textbox

第一次检查时,我推测这将是一项简单的任务(也许现在仍然如此!),但我遇到了困难。假设我有一个包含 1000 个文本框的表单,其中每个文本框包含随机分布的但在许多情况下匹配的字符串。例如,如果有 1000 个文本框,则可以在任何一个中找到以下内容:

AAAA-XXXX
AAAA-XXXX
BBBB-XXXX
BBBB-XXXX
CCCC-XXXX
CCCC-XXXX
  ...

我如何遍历文本框、识别所有匹配的示例并突出显示匹配出现的 textbox.backcolor?完全匹配的背景色应该相同,但每组独特的匹配项的背景色应该不同。可能有多达 100 个不同的集合!

你可以做到

Dim strText As String
    For Each TextBox1 As System.Windows.Forms.TextBox In Me.Controls
        strText = TextBox1.Text
        For Each TextBox2 As System.Windows.Forms.TextBox In Me.Controls
            If strText = TextBox2.Text AndAlso TextBox2.Name <> TextBox1.Name Then
                TextBox2.BackColor = Color.Red ' or whatever you want to use
                TextBox1.BackColor = Color.Red
            End If

        Next
    Next

刚刚拼凑了一个工作测试项目,这就是我会采用的方法。它避免将每个文本框与其他每个文本框进行比较。我已经为你评论完了:)

Private Sub SetBoxColors()

    'The keys are textbox texts, the values are the number of times it occurs
    Dim UniqueTextsAndUsage As New Dictionary(Of String, Integer)

    Dim FirstInstanceTextBoxes As New List(Of TextBox)

    'The keys are textbox texts, the values are the colour for the box
    Dim UniqueColors As New Dictionary(Of String, System.Drawing.Color)

    'Iterate over all the text boxes
    ' Substitute Me for your Form instance if necessary
    For Each TBox As Control In Me.Controls

        'Skip things that aren't textboxes
        If Not TypeOf TBox Is TextBox Then
            Continue For
        End If

        'If we have seen this textbox text before
        If UniqueTextsAndUsage.ContainsKey(TBox.Text) Then

            'Increase the usage
            UniqueTextsAndUsage(TBox.Text) += 1

            If UniqueTextsAndUsage(TBox.Text) = 2 Then

                'This is the second usage, generate a colour for this set of boxes
                UniqueColors.Add(TBox.Text, GenerateColor(UniqueColors.Count + 1))

            End If

            'Colour this textbox
            ' (it won't get the first instance of each unique string)
            TBox.BackColor = UniqueColors(TBox.Text)

        Else

            'We have NOT seen this textbox text before

            'Add the first occurence of the text
            UniqueTextsAndUsage.Add(TBox.Text, 1)

            'Mark this textbox as one we may have to colour later
            FirstInstanceTextBoxes.Add(TBox)

        End If

    Next

    'Colour all the first instances
    For Each TBox As TextBox In FirstInstanceTextBoxes

        'Check there are sufficient uses of this text
        If UniqueTextsAndUsage(TBox.Text) > 1 Then
            TBox.BackColor = UniqueColors(TBox.Text)
        End If

    Next

End Sub

Private Function GenerateColor(Id As Integer) As System.Drawing.Color

    'Needs more thought - often too dark
    Dim KnownColourByIdNumber As System.Drawing.KnownColor = Id
    Return System.Drawing.Color.FromKnownColor(KnownColourByIdNumber)

End Function

GenerateColor 函数需要更多考虑,以便它生成一些更好的颜色,但我把它留给你了。

您可能还需要将每个框设置为 DefaultControl 颜色或任何名称的重置,以及 运行 SetBoxColors 顶部的设置。