Lets see if I can explain-

I have a text box (rich text box) in a form. Inside the box, some data gets put in there- specifically insurance information.

Normally, the box doesn't need to change color to alert the user there's a problem with the text that's inside, because the information is usually good.

But there are a few phrases that need to alert the user there's a problem.

For example, if the words "Not Found AS OF:" appear anywhere in the box, I need the background to show red. Or, if the words "Letter #1" appear in the box, it needs to show red. Absent of any key words, the box is normal (white).

I thought a simple If/Else statement would do the trick, but it's not.

If txtInsurance.Text = "Not Found: AS OF" Then
            txtInsurance.ForeColor = Color.Red
        Else
            txtInsurance.ForeColor = Color.Black
        End If

I know that says ForeColor, I'm playing around with fore vs BackColor.

Can anyone help?

Dani AI

Generated

Brief summary: the original problem from is to change a textbox/RichTextBox background when certain phrases appear. 's selection/highlight approach solved the immediate need to mark offending words. If the goal is only to flag the whole control (red background when any trigger phrase exists), a simpler, more robust check is preferable. If per-word highlighting is required, add a few defensive steps (save selection, avoid visible flicker, handle case and partial matches).

A concise VB.NET pattern that checks multiple keywords (case-insensitive) and sets the whole control BackColor:

' run after text is assigned or on TextChanged
Dim keywords() As String = {"Not Found AS OF", "Letter #1"}
Dim txtLower As String = txtInsurance.Text.ToLowerInvariant()
Dim found As Boolean = False

For Each k As String In keywords
    If txtLower.Contains(k.ToLowerInvariant()) Then
        found = True
        Exit For
    End If
Next

txtInsurance.BackColor = If(found, Color.LightCoral, Color.White)

Notes for the RichTextBox/highlight route (the approach from ): preserve user selection/caret while scanning, and avoid visible temporary selections. Save SelectionStart/SelectionLength, perform finds/formatting, then restore selection. Example pattern for saving/restoring around a scan:

Dim selStart As Integer = richTextBox1.SelectionStart
Dim selLen As Integer = richTextBox1.SelectionLength

' perform find-and-format passes here (check for -1 from Find and loop safely)

richTextBox1.SelectionStart = selStart
richTextBox1.SelectionLength = selLen

For ASP.NET (browser) scenarios, use client-side JavaScript to give instant feedback (or set TextBox.BackColor on server before render). Example client-side pattern:

<script>
var el = document.getElementById('txtInsurance');
el.addEventListener('input', function(){
  var v = el.value.toLowerCase();
  el.style.backgroundColor = (v.indexOf('not found as of') !== -1 || v.indexOf('letter #1') !== -1) ? '#fdd' : '#fff';
});
</script>

Cautions: prefer case-insensitive checks or regex with word boundaries for precise matches; always guard loops against -1 results; and pick whole-control BackColor when a simple flag is sufficient to avoid complex selection manipulation.

Recommended Answers

All 2 Replies

In richtextbox1.TextChanged Event you should check that it contains your desired text or not, for that use .FIND() method to get index of it, then select then whole keyword and color it like this:

Dim i As Integer = richtextbox1.Find("Not Found AS OF:") 'See other constructors also
richtextbox1.Select(i, "Not Found AS OF:".Length)
richtextbox1.SelectionColor = Color.Black  'Set ForeColor
commented: Thanks for the quick reply! +1

Thanks ShahanDev for your quick reply. Your solution worked for me.

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.