|
-
May 16th, 2006, 11:50 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Text box color change if the value is in negative!!
Hi,
I have a simple querry here. There is a txtbox (txtTotal). What I need to know is, if there is a positive value in txtTotal (Ex. 25) the txtbox backcolor will be in blue. It a negative value in that txtbox (Ex. -25) its backcolor should be in Red color. txtTotal values are not constant values.
How can I do this?
Regards,
Seema_S
-
May 17th, 2006, 03:22 AM
#2
Hyperactive Member
Re: Text box color change if the value is in negative!!
off the top of my head:
VB Code:
Private Sub Command1_Click()
If Text1.Text < 0 Then
Text1.BackColor = vbRed
Else
Text1.BackColor = vbBlue
End If
End Sub
-
May 17th, 2006, 05:41 AM
#3
Thread Starter
Fanatic Member
Re: Text box color change if the value is in negative!!
Hi,
I am getting only the red color in both cases whether it is a negative value or positive value.
VB Code:
Public Sub ColorChange()
If Text1.Text < 0 Then
Text1.BackColor = vbRed
Else
Text1.BackColor = vbBlue
End If
End Sub
[/QUOTE]
Seema_S
-
May 17th, 2006, 05:44 AM
#4
Re: Text box color change if the value is in negative!!
 Originally Posted by whythetorment
off the top of my head:
VB Code:
Private Sub Command1_Click()
If Text1.Text < 0 Then
Text1.BackColor = vbRed
Else
Text1.BackColor = vbBlue
End If
End Sub
Close. But, remember, the contents of a textbox, by default, is a string. In order to evaluate it numerically, it must be converted.
VB Code:
Private Sub Command1_Click()
If Val(Text1.Text) < 0 Then
Text1.BackColor = vbRed
Else
Text1.BackColor = vbBlue
End If
End Sub
-
May 17th, 2006, 05:50 AM
#5
Re: Text box color change if the value is in negative!!
This works for me and I added the ForeColor code to make the text value a little easier on the eyes:
VB Code:
Private Sub Command1_Click()
With Text1
If .Text < 0 Then
.BackColor = vbRed
.ForeColor = vbWhite
Else
.BackColor = vbBlue
.ForeColor = vbWhite
End If
End With
End Sub
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
May 17th, 2006, 06:08 AM
#6
Thread Starter
Fanatic Member
Re: Text box color change if the value is in negative!!
Thanks for the help. It works fine.
Seema_S
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|