Results 1 to 7 of 7

Thread: [RESOLVED] Coloring Text after percentage calculation

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [RESOLVED] Coloring Text after percentage calculation

    Hello,

    I'm calculate the persentage between two numbers.
    After the calculation I want that the positive numbers are green and the others red.

    It's working good, but the numbers -0.25 are still green instead of red.
    How can I change that?

    Here's my code for calculation and coloring the text:

    Code:
      Dim C1 As String = TextBox11.Text
            Dim D1 As String = TextBox1.Text
            Dim percentage As Decimal = Val(D1 - C1) / C1 * 100
    
            TextBox16.Text = String.Format("{0:0.00}", percentage) & " " & "%"
    
            If Val(TextBox16.Text) >= 0.00 Then
    
                TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
                TextBox16.ForeColor = Color.DarkGreen
            Else
    
                TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
                TextBox16.ForeColor = Color.Red
               
            End If
    Thanks in advance,
    Last edited by sparrow1; Nov 10th, 2020 at 09:12 AM.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,151

    Re: Coloring Text after percentage calculation

    There's a lot to unpack there... so I'll try to break it down:
    Code:
    Dim C1 As String = TextBox11.Text
    Dim D1 As String = TextBox1.Text
    First, C1 and D1 are strings... but you then use them for mathematical calculations. They should be Decimal at the least, and you should use Decimal.TryParse() to convert the text into numbers.


    Code:
    Dim percentage As Decimal = Val(D1 - C1) / C1 * 10
    By the time you use Val here, it's too late... at the least it should be (Val(D1) - Val(C1)) ....

    Code:
    If Val(TextBox16.Text) >= 0.00 Then
    Here you are using Val again... when you calculated percentage in the line above.... Why not use it again here?

    Which brings me to the final thing: don't use Val... it has issues, and one of those is what you're running into.
    Val returns all numerics up to the first non-numeric character... so Val("123.456") returns 123.... Val("C123") returns 0.... and Val("-0.25") returns....... 0. The "-" doesn't register as a numeric. That's why you should avoid it unless you know what you're dealing with. Use .TryPArse instead.

    Code:
    Dim C1, D1 as Decimal
    Decimal.TryParse(Textbox11.text, C1)
    Decimal.TryParse(Textbox1.text, D1)
    Dim percentage as DEcimal = (D1-C1)/C1 * 100
    
    TextBox16.text = String.Format("{0:0.00} %", percentage)
    
    If percentage >= 0.00 then
      TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
      TextBox16.ForeColor = Color.DarkGreen
    Else
      TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
      TextBox16.ForeColor = Color.Red
    End If
    I think I have that right... been a while since I've used TryPArse though...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,666

    Re: Coloring Text after percentage calculation

    Quote Originally Posted by techgnome View Post
    There's a lot to unpack there... so I'll try to break it down:
    Code:
    Dim C1 As String = TextBox11.Text
    Dim D1 As String = TextBox1.Text
    First, C1 and D1 are strings... but you then use them for mathematical calculations. They should be Decimal at the least, and you should use Decimal.TryParse() to convert the text into numbers.


    Code:
    Dim percentage As Decimal = Val(D1 - C1) / C1 * 10
    By the time you use Val here, it's too late... at the least it should be (Val(D1) - Val(C1)) ....

    Code:
    If Val(TextBox16.Text) >= 0.00 Then
    Here you are using Val again... when you calculated percentage in the line above.... Why not use it again here?

    Which brings me to the final thing: don't use Val... it has issues, and one of those is what you're running into.
    Val returns all numerics up to the first non-numeric character... so Val("123.456") returns 123.... Val("C123") returns 0.... and Val("-0.25") returns....... 0. The "-" doesn't register as a numeric. That's why you should avoid it unless you know what you're dealing with. Use .TryPArse instead.

    Code:
    Dim C1, D1 as Decimal
    Decimal.TryParse(Textbox11.text, C1)
    Decimal.TryParse(Textbox1.text, D1)
    Dim percentage as DEcimal = (D1-C1)/C1 * 100
    
    TextBox16.text = String.Format("{0:0.00} %", percentage)
    
    If percentage >= 0.00 then
      TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
      TextBox16.ForeColor = Color.DarkGreen
    Else
      TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
      TextBox16.ForeColor = Color.Red
    End If
    I think I have that right... been a while since I've used TryPArse though...

    -tg
    Code:
    Dim C1, D1 as Decimal
    if Decimal.TryParse(Textbox11.text, C1) AndAlso Decimal.TryParse(Textbox1.text, D1) Then
        Dim percentage as Decimal = (D1-C1)/C1 * 100
    
        TextBox16.text = String.Format("{0:0.00} %", percentage)
    
        If percentage >= 0.00 then
            TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
            TextBox16.ForeColor = Color.DarkGreen
        Else
            TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
            TextBox16.ForeColor = Color.Red
        End If
    Else
        TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
        TextBox16.ForeColor = Color.Black
        TextBox16.text = "Invalid input"
    End If

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,252

    Re: Coloring Text after percentage calculation

    Quote Originally Posted by techgnome View Post
    ... so Val("123.456") returns 123.... Val("C123") returns 0.... and Val("-0.25") returns....... 0. The "-" doesn't register as a numeric. ...
    Only one of those examples is true.
    Val returns a Double, so Val("123.456") returns a double set to 123.456, and Val("-0.25") returns a double set to -0.25.
    It isn't locale sensitive, so if you use commas instead of periods for the decimal point, it won't convert correctly.

    I'm not saying to use Val, as it is a holdover from legacy VB, but to think that a decimal point (which must be a period) and a negative sign are not considered part of a numeric string is rather a surprising belief to hold.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Coloring Text after percentage calculation

    I'ts working fine.
    The Textbox is coloring like I wanted.
    The're is however a problem after closing and reopening the form.
    Then the Textbox is coloring Black.

    What can I do to save those colors?
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    490

    Re: Coloring Text after percentage calculation

    You need to save the information at an upper level. if you create an instance of the form (initialization of all variables), deal with it (modify your variables and properties) and close it (reset all value) , you remove all information that are not in the initial properties of the form. So you need to save the information you want in variables external to that form, so you can keep them and call them when you load the form.

    or you can also hide the form instead of closing it so the instance is still the same and the value are not reset.

    regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  7. #7

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Coloring Text after percentage calculation

    Hello,

    After some search I added this code to the Form Load.

    Code:
    Dim percentage as Decimal = Val(Textbox16.Text)
    
        If percentage >= 0.00 then
            TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
            TextBox16.ForeColor = Color.DarkGreen
        Else
            TextBox16.Font = New Font("Adobe Devanagari", 11.0, FontStyle.Bold)
            TextBox16.ForeColor = Color.Red
        End If
    Thanks for helping.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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