How do I conditionally format a TextBox?



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here




From: David M. Camp
Date: Friday 08 June 2001 12:11 AM
Subject: Re: Visual Basic 6 Help
Question: I do have another question if I may ask?

I am displaying in a TEXT-BOX a field from one of my databases. I know how to change the property of the text-box to display the text in different colors. But what I want is that when the text is (Let's say positive it is in black and when it is a negative it will display red). Can you help me on this matter?

Once again thanks for all your help and insight. David M. Camp

Answer by Martin Allen:

Is this just a normal TextBox you are talking about?  If it is, then you can do it with this code:

Private Sub Text1_Change()
If IsNumeric(Text1.Text) = False Then
    Text1.ForeColor = RGB(255, 0, 0) 'If code is not numeric, set it to red
Exit Sub
End If

If Text1.Text >= 0 Then
    Text1.ForeColor = &H80000008 'If text is greater than or equal to zero, then set to default text colour
    Text1.ForeColor = RGB(255, 0, 0) 'If text is less than zero, set to red
End If
End Sub

Archived Comments

Date: Thursday, May 24, 2007 at 11:44:48
Comments: Thanks a lot Mr.Martin Allen and your source code works according to specification.

Martin Allen 1999 - 2011.  Last updated Thursday 11 August 2011 08:46:41 PM +0100.