-
Nov 10th, 2020, 09:04 AM
#1
[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.
-
Nov 10th, 2020, 09:44 AM
#2
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
-
Nov 10th, 2020, 04:23 PM
#3
Re: Coloring Text after percentage calculation
Originally Posted by techgnome
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 11:58 PM
#4
Re: Coloring Text after percentage calculation
Originally Posted by techgnome
... 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
-
Nov 13th, 2020, 06:12 AM
#5
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?
-
Nov 13th, 2020, 09:35 AM
#6
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)
“They did not know it was impossible so they did it” (Mark Twain)
-
Dec 2nd, 2020, 04:23 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|