Click here to Skip to main content
16,020,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, like title, I have a timer, that when reach 0 will fire event on another form, then timer restart, this seem to work if it's >=0 but timer can't go down except some more click, and timer fire the event but I think that in this way it's like a normal button. how can I achieve this?

What I have tried:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       If Label10.Text >= "1" <= "30" Then
           Label10.Text = Label10.Text - 1.ToString
       ElseIf Label10.Text = "0" Then
           Form11.Button5_Click(sender, e)
       End If
   End Sub
Posted
Updated 17-Jan-22 10:16am
v4

1 solution

That code won't even compile so everything else you said about it doing something doesn't matter.

This comparison is not possible and will keep your code from compiling:
VB.NET
If Label10.Text >= "1" <= "30" Then

First, the content of a label is always a string, so you're not comparing the value of the label for something between 1 and 30.

Next, you MUST have a logical operator between comparisons. You could should look more like this:
VB.NET
Dim value = CInt(Label10.Text)
If value >= 1 And value <= 30 Then

But even that's bad. The label should just be used to displaying a value, NOT being used to store one and be made part of your apps logic.
 
Share this answer
 
Comments
Richard Deeming 18-Jan-22 4:25am    
Actually, it will compile; it just won't do what the OP expects.

It compiles to:
if ((Microsoft.VisualBasic.CompilerServices.EmbeddedOperators.CompareString(Label10.Text, "1", false) >= 0) ? 1 : 0) >= (Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean("30") ? 1 : 0))


CompareString(x, "1", false) will return True if the string starts with a character which is greater than or equal to "1" (ASCII code 49).

Conversions.ToBoolean("30") will always return true.

So the If branch will be executed for "30", "9999", "Is this code broken?", etc.

Evil type coercion: yet another reason why VB sucks.
Dave Kreskowiak 18-Jan-22 7:11am    
Wow. It's been quite a long time since I did VB.NET.

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