2 Replies - 3380 Views - Last Post: 17 July 2013 - 02:09 PM Rate Topic: -----

#1 friendo9876   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 13

C# script to change color of text based on value only returns green

Posted 17 July 2013 - 01:37 PM

have this c# script that is changing the color of the text based on the value of TWA. While it works mostly, it doesn't change if the

TWA value is >=85 (where it should be yellow) and >= 90 (where it should be red).

How to fix this?

Here is the script:
protected System.Drawing.Color GetColorForLabel(string text)
    int theTWAValue;
    if (text != null && int.TryParse(text, out theTWAValue) && theTWAValue >= 0)
        return (theTWAValue < 90) ? System.Drawing.Color.Yellow : System.Drawing.Color.Red;

    return System.Drawing.Color.Green;

additional info:The listview with the TWA value is not going to be shown until the user selects certain job code from the drop down list and all the data values are coming from an access database. I also have tried debugging and have had no success. For some reason the if statement is failing

Is This A Good Question/Topic? 0
  • +

Replies To: C# script to change color of text based on value only returns green

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# script to change color of text based on value only returns green

Posted 17 July 2013 - 02:05 PM

What is the run-time value of theTWAValue on line 6?
HINT: Put in a breakpoint to check it.
Maybe show us a screen shot of that run-time value so we all know you know what we're talking about.

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

Did you even read about the command you're trying to use, and look at the examples?
C# Language Specification

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated July 2013

Working for myself/Deploying/Selling software:

  • So you want to be a game programmer...

  • Q:How do I stay a free lance coder for a living?

  • Q: How do I become a better coder?
    Read this, and practice a lot.
    How to be a better coder

  • Q: Are there any resources for small C# projects a novice to become a better coder?

  • Q: How do I sell the software I've developed?

  • Q: How do I decide how much to charge for my application?
    A: Read this
    A lot of questions about freelancing were covered in the Q&A with the Experts thread

  • Q: How do I deploy my program / Make an installer?

  • Was This Post Helpful? 0
    • +
    • -

    #3 Martyr2   User is offline

    • Programming Theoretician
    • member icon

    Reputation: 5554
    • View blog
    • Posts: 14,560
    • Joined: 18-April 07

    Re: C# script to change color of text based on value only returns green

    Posted 17 July 2013 - 02:09 PM

    Well there is nothing syntactically wrong with it. If you are testing greater than or equal to 85 you should have theTWAValue >= 85. Otherwise, the way you have it, I could still trigger yellow by having a value like 4.

    Another thing you should look at is trying to parse the value first, THEN test its value rather than trying to parse it in the middle of a boolean condition. That way if it fails the parsing, done deal. You can plan what to do next. This isn't to say that the way you have it won't work, it is just more solid to first try to parse the value and then if that succeeds, go into testing its result.

    The code itself works as it is suppose to. So it is how you call this function and the code involved there that is more likely the issue. Can you show us code of how you are calling this? My guess is that the number you are passing to this function is not what you think it is and it is not parsing correctly. If it doesn't parse it is going to fail your if statement and keep returning green. Check the value coming into this function.

    Was This Post Helpful? 0
    • +
    • -

    Page 1 of 1