Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Right now, as the code is written, when I take write in textbox 10.2 as an input and mass is 10, then the difference is 0.2 which is less than 0.5 but i still get red color which means it is not true, so i think that it still doesnt recognize input as a double or something similar. Does anyone know what might be an issue. Besides that the code works just fine.

What I have tried:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim lineNumber0 As Integer
    Dim mass As Double
    Dim input As Double

    If Double.TryParse(File.ReadAllLines("225.txt").ElementAt(linenumber0), mass) Then
        If Double.TryParse(TextBox1.Text, input) AndAlso Math.Abs(input - mass) < 0.5 Then
            TextBox1.BackColor = Color.Green      
    Else
        textbox1.backcolor=color.red
    End If
   End if
Posted
Comments
Richard Deeming 13-Mar-20 13:05pm    
You haven't initialized the lineNumber0 variable, so you're always getting the first line of the file. Are you sure that's the value you're intending to compare the input to?
Richard MacCutchan 13-Mar-20 13:12pm    
Don't write compound statements like that, as it only helps to complicate matters; as you can clearly see. Split things into logical steps, something like:
1. Read the contents of the file and find the string that contains the first number.
2. Convert the string to a Double value, not forgetting to take action of the conversion fails.
3. Do the same for the string in the TextBox.
4. Compare the two numbers and take action(s) accordingly.
Marc Kane 13-Mar-20 18:35pm    
Thanks, will try to use these advices

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900