By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

How do I change backcolor of textbox based on the value inside it?

P: 14
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         If txtInsurance.Text = "Not Found: AS OF" Then
  3.             txtInsurance.ForeColor = Color.Red
  4.         Else
  5.             txtInsurance.ForeColor = Color.Black
  6.         End If
  7.  
  8.  
  9.  
I know that says ForeColor, I'm playing around with fore vs BackColor.

Can anyone help?
Dec 31 '10 #1

✓ answered by David Gluth

Use the “contains” function. Be very specific in your search text. For example your text says “if the words "Not Found AS OF:"” while your sample code says “"Not Found: AS OF" Notice the location of the “:” in the two – they are not alike and would not be found. Visual basic compares are case sensitive so you should also change the text to either all upper or all lower before comparing
Expand|Select|Wrap|Line Numbers
  1. If TextBox1.Text.ToUpper.Contains("NOT FOUND AS OF") Then
  2.             TextBox1.BackColor = Color.Red
  3.         End If
  4.  

Share this Question
Share on Google+
2 Replies


P: 46
Use the “contains” function. Be very specific in your search text. For example your text says “if the words "Not Found AS OF:"” while your sample code says “"Not Found: AS OF" Notice the location of the “:” in the two – they are not alike and would not be found. Visual basic compares are case sensitive so you should also change the text to either all upper or all lower before comparing
Expand|Select|Wrap|Line Numbers
  1. If TextBox1.Text.ToUpper.Contains("NOT FOUND AS OF") Then
  2.             TextBox1.BackColor = Color.Red
  3.         End If
  4.  
Dec 31 '10 #2

P: 14
David- thanks for your quick reply. Your solution worked for me, and was a good suggestion.
Jan 1 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.