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?

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.