MalLav
MalLav

Reputation: 45

Highlight sets of matches within textbox

Upon first inspection, I surmised this would be a straightforward task (maybe it still is!), yet I am having difficulty. Suppose I have a form with 1000 textboxes, where each textbox contains randomly distributed, yet in many cases matching strings. For example, the below may be found in any one if the 1000 textboxes:

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

How might I loop through the textboxes, identify all the matching examples and highlight the textbox.backcolor where the matches occur? The backcolor should be the same for the exact matches, yet different for each unique set of matches. There could be as many as 100 different sets!

Upvotes: 0

Views: 219

Answers (2)

Ste Griffiths
Ste Griffiths

Reputation: 318

Just whipped together a working test project, this is the kind of approach I'd take. It avoids comparing every textbox to every other textbox. I've commented it all up for you :)

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

The GenerateColor function needs more thought so that it generates some nicer colours but I leave that to you.

You may also need a reset that sets every box to the DefaultControl color or whatever it's called, and run that at the top of SetBoxColors.

Matching text colours

Upvotes: 1

Leprechaun
Leprechaun

Reputation: 349

You can do this

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

Upvotes: 1

Related Questions