Team -

Working on a personal project to help with my sons spelling.

I can't seem to find an easy solution to this so I hope someone can point me in the right direction.

Textbox 1 will hold the correctly spelled word.
Textbox 2 will be typed BUT I want the background of textbox2 to change green if he is on track but turn red when he messes up on a letter.

Basically I need the two boxes to be validated each time he presses a key. I can do the box to box when complete but the trick I am having is how to I have them check as he is typing?

Thanks!

Dani AI

Generated

Nice, practical idea and a tidy answer from — checking the typed text continuously is exactly the right direction. A few common refinements make the exercise more robust for a kid: handle case and stray spaces, give per-character feedback if you want to highlight only the wrong letter, and optionally soften feedback for tiny typos.

Normalize and compare only the portion you need (case-insensitive, trimmed). Run this logic whenever the input changes and treat the typed text as the prefix to validate:

Dim target As String = correctWord.Trim()
Dim entered As String = typedText.Trim()

If entered.Length = 0 Then
    ' neutral UI
Else
    Dim prefix As String = target.Substring(0, Math.Min(entered.Length, target.Length))
    If String.Compare(prefix, entered, True) = 0 Then
        ' matched so far -> green
    Else
        ' mismatch -> red
    End If
End If

If per-character highlighting is preferred (so only the wrong character is colored), use a RichTextBox and its selection color rather than a plain TextBox. Find the first mismatched index, reset formatting, then color just that character:

Dim mismatch As Integer = -1
For i As Integer = 0 To Math.Min(entered.Length, target.Length) - 1
    If Char.ToLowerInvariant(entered(i)) <> Char.ToLowerInvariant(target(i)) Then
        mismatch = i
        Exit For
    End If
Next

rtb.SelectAll()
rtb.SelectionBackColor = SystemColors.Window

If mismatch <> -1 Then
    rtb.SelectionStart = mismatch
    rtb.SelectionLength = 1
    rtb.SelectionBackColor = Color.Salmon
End If
rtb.SelectionStart = entered.Length
rtb.SelectionLength = 0

Other tips: set MaxLength to the target word to prevent extra letters, handle paste operations (users may paste instead of typing), and consider fuzzy matching (Levenshtein distance) to give "almost correct" feedback before marking a hard red. For a kid-friendly app, add gentle audio or a progressive score so mistakes become learning moments rather than harsh failures.

Recommended Answers

All 2 Replies

Use the TextChanged event.

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

        If (TextBox1.Text.StartsWith(TextBox2.Text)) Then
            TextBox2.BackColor = Color.LightGreen
        Else
            TextBox2.BackColor = Color.Salmon
        End If

    End Sub

That works exactly right. Thanks for the .StartsWith, I had never heard of it before!

You just saved me tons of time. I was working with determining lengths and doing trims based on lengths etc. and have the two boxes compare letter by letter but this is MUCH easier.

Thank you again!
Dewayne

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.