martin2k

Google
 
Web www.martin2k.co.uk

How do I conditionally format a TextBox?


 

Home

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

Contact


 

Question

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
Else
    Text1.ForeColor = RGB(255, 0, 0) 'If text is less than zero, set to red
End If
End Sub


Comments

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

Please fill in the below form if you have any comments or additional information you want to add about the above information.  If you have any Visual Basic 6.0 questions, please visit the ForumAny questions sent using this form will be ignored.

* - Mandatory

*Name: E-mail:

*Comments:

*Please type the following code (your comment will not be accepted without it):


Martin Allen 1999 - 2007.  Last updated Sunday 12 August 2007 06:00:55 PM +0100.