Results 1 to 6 of 6

Thread: [RESOLVED] Text box color change if the value is in negative!!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Resolved [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

  2. #2
    Hyperactive Member
    Join Date
    May 2005
    Posts
    307

    Re: Text box color change if the value is in negative!!

    off the top of my head:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Text1.Text < 0 Then
    3.         Text1.BackColor = vbRed
    4.     Else
    5.         Text1.BackColor = vbBlue
    6.     End If
    7. End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    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:
    1. Public Sub ColorChange()
    2.     If Text1.Text < 0 Then
    3.         Text1.BackColor = vbRed
    4.     Else
    5.         Text1.BackColor = vbBlue
    6.     End If
    7. End Sub
    [/QUOTE]

    Seema_S

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Text box color change if the value is in negative!!

    Quote Originally Posted by whythetorment
    off the top of my head:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Text1.Text < 0 Then
    3.         Text1.BackColor = vbRed
    4.     Else
    5.         Text1.BackColor = vbBlue
    6.     End If
    7. 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:
    1. Private Sub Command1_Click()
    2. If Val(Text1.Text) < 0 Then
    3.    Text1.BackColor = vbRed
    4. Else
    5.    Text1.BackColor = vbBlue
    6. End If
    7. End Sub

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    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:
    1. Private Sub Command1_Click()
    2.     With Text1
    3.         If .Text < 0 Then
    4.             .BackColor = vbRed
    5.             .ForeColor = vbWhite
    6.         Else
    7.             .BackColor = vbBlue
    8.             .ForeColor = vbWhite
    9.         End If
    10.     End With
    11. 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."


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    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
  •  



Click Here to Expand Forum to Full Width