【问题标题】:Highlight sets of matches within textbox突出显示文本框中的匹配集
【发布时间】:2016-04-29 15:00:43
【问题描述】:

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

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

我如何遍历文本框,识别所有匹配的示例并突出显示匹配的 textbox.backcolor ?精确匹配的背景色应该相同,但每个唯一匹配集的背景色不同。可能有多达 100 个不同的集合!

【问题讨论】:

  • For x = 1 To 1000Me.Controls("TextBox" & x)?

标签: vb.net string textbox compare


【解决方案1】:

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

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 的顶部运行它。

【讨论】:

  • 感谢您的精彩回复!我从中学到了很多东西,它几乎完全符合我的需要。我会努力使它适合我的形式。谢谢!
【解决方案2】:

你可以这样做

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

【讨论】:

  • 感谢您的帮助!我从类似的角度解决了这个问题 - 这将正确识别匹配项,但如何为匹配项赋予独特的颜色?无论 textbox.text 是什么,您的示例中的所有匹配项都会设置背景色。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 2023-01-08
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多