[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
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
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
Re: Text box color change if the value is in negative!!
Quote:
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
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
Re: Text box color change if the value is in negative!!
Thanks for the help. It works fine.
Seema_S