Results 1 to 7 of 7

Thread: [RESOLVED] ListBoxItem colour and percentages

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Resolved [RESOLVED] ListBoxItem colour and percentages

    Hi guys!

    I've got a list box full of items that have percentage values. What I need to do is colour code the background of the listbox items depending on the value of the percentage smoothly from green (being 100%) to red (0%).

    I can't seem to think how to do this, so advice or an existing piece of code would be a great help.

    Thanks!
    My Blog.

    Ryan Jones.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ListBoxItem colour and percentages

    You use the term "ListBoxItem" in the thread title. Does that mean that this is an ASP.NET application, because that's the only place you would find a ListBoxItem object, other than an upgraded VB6 app. If this is a web app then you should tell us or, better yet, post in the ASP.NET forum, which is designed for ASP.NET questions. If it's not a web app then you must not be using ListBoxItem objects, so you're thread title is misleading. It's very important to be clear so that we don't waste our time and yours with irrelevant answers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: ListBoxItem colour and percentages

    it's actually WPF. But since the code is not about WPF but about generating a background colour using C# code I didn't think it would matter.

    However if anyone feels differently feel free to move the thread elsewhere.

    To be clear I was thinking of something along the lines of:

    100% = completely green
    0% = completely red

    And between the the two the colour is faded from one to the other depending on the percentage.
    Last edited by sciguyryan; May 2nd, 2010 at 10:57 AM.
    My Blog.

    Ryan Jones.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ListBoxItem colour and percentages

    It's simple mathematics. A Color is created from red, blue and green components in the value 0 to 255. Use 0 for the blue, a percentage of 255 for the green and 100 less that percentage of 255 for the red.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: ListBoxItem colour and percentages

    I know that part. I just can't seem to get the correct formula to face from 100% green too 100% red depending on the percentage.

    I don't suppose you know how?
    My Blog.

    Ryan Jones.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ListBoxItem colour and percentages

    You don't know how to calculate a percentage of 255?
    csharp Code:
    1. double percentage; // e.g. 78.2
    2. double green = 255.0 * percentage / 100.0;
    3. double red = 255.0 * (100.0 - percentage) / 100.0;
    You round and convert to bytes and pass the results to Color.FromRgb.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: ListBoxItem colour and percentages

    I do. Just I've never used them like that before. I think I had a case of over complication since I was ending up using something that was 3 lines long... D'oh. Thanks
    My Blog.

    Ryan Jones.

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