# Help with VB 2010 Express.



## Roger ashley (Apr 14, 2008)

Hi,
For many years I have had a program running to water my garden, written in VB6... I have had to upgrade my PC to Win 7. Now I find that VB6 doesn't like me at all. I have downloaded 2010 express and although I have looked everywhere I can think of, I can't find a solution that works. My problem is that I need to have a label showing the PC time in HH:MM, changing as the PC changes. I then have to compare a time put into a textbox to the PC time and cause a timer to start. I have all the rest working, and can put a time up but it doesn't follow the PC time.
I'm near 70 and grey, so I can't afford to loose too much more hair, pulling it out in frustration:sad:
Thanks in advance to whoever can point me in the right direction.


----------



## niemiro (Feb 20, 2010)

Hello!

Drag on a new Timer Control onto your Form1 [Design]. Set the following properties:

Enabled: True
Interval: 10000

Now double click on the Timer1 control, and inside the Tick event, set the label1.Text value to the current time as you normally would.

If you have any more questions, please do not hesitate to ask 

Richard


----------



## Roger ashley (Apr 14, 2008)

Hi Richard,
I some how got it to work ok. But now the simplest of items has me in a spin
I want to compare the time as "HHmm" in the label box with the entered time in the TextBox "HHmm" and to when the two are equal, for the label 14 to show state: If that makes sence to you. I have a number of books on VB6 but haven't set myself up with 2010 yet

Private Sub TextBox1_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

If Label36.Text = TextBox1.Text Then
Label40.Text = "ON"
Else
Label14.Text = "OFF"

End If

End Sub

Many thanks for trying to help.
Roger.


----------



## niemiro (Feb 20, 2010)

Hello Roger,

You are doing very well  Visual Basic is very different to VB6.

The code you have written:


```
If Label36.Text = TextBox1.Text Then
Label40.Text = "ON"
Else
Label14.Text = "OFF"
End If
```
is correct. However, you need to move it out of the TextBox1_Changed sub, and into the Timer1_Tick sub.

The Timer1_Tick sub is called every 10 seconds (with an interval of 10,000 at any rate - interval is measured in milliseconds).

I hope this helps, and the very best of luck!

Richard


----------



## Roger ashley (Apr 14, 2008)

Hi,
Will try that after my wife has fed and watered me. Just out of interest, what is the longest time that the timers can be set to?
All the best
Roger


----------



## niemiro (Feb 20, 2010)

Roger ashley said:


> Hi,
> Will try that after my wife has fed and watered me. Just out of interest, what is the longest time that the timers can be set to?
> All the best
> Roger


No problem. There is no rush 

The max value is 2,147,483,647 milliseconds, which equals:

24.86 days
596.5 hours
35, 791 minutes
2,147,483.647 seconds

596 hours, 31 minutes, 23.65 seconds.

N.B. This is the interval time (the time between ticks). There is no limit on how many times it may tick.

The reason for this odd number is all due to the maximum number you can store in binary in 32bits (32 1s or 0s). It looks neater in hexadecimal: 0x7FFFFFFF. Int32.MaxValue Field (System)

Richard


----------



## Roger ashley (Apr 14, 2008)

Morning Richard,
All is working fine, your help was invaluable. During those moments of thinking, I wondered if there is a way that a timer can be set with a textbox time? i.e so that a user can set a time in minutes between operations "a" and "b".
Spent a good hour looking through my VB6 books but couldn't find the answer.
Or is it case of taking the original start time, i/p a number of minutes, adding that i/p to the original to give an event. Hope that makes sense.
All the best
Roger.


----------



## niemiro (Feb 20, 2010)

Roger ashley said:


> Morning Richard,
> All is working fine, your help was invaluable. During those moments of thinking, I wondered if there is a way that a timer can be set with a textbox time? i.e so that a user can set a time in minutes between operations "a" and "b".
> Spent a good hour looking through my VB6 books but couldn't find the answer.
> Or is it case of taking the original start time, i/p a number of minutes, adding that i/p to the original to give an event. Hope that makes sense.
> ...


And good morning to you too 

I am glad to have been of assistance. 

There are a couple of ways of achieving what you want.

Most simply, you could set the Timer's Interval property to what the user entered (remember that it is in milliseconds, so a conversion from the entered minutes is required)


```
Try
        Timer1.Interval = Convert.ToInt32(TextBox1.Text) * 60000
Catch ex as FormatException
        MsgBox("Only numeric characters may be entered into TextBox1")
End Try
```
This extra code is required, because TextBoxes allow both numbers and letters to be entered. In order to do multiplication, and use it as a number, the entered text must be validated and converted to something the program knows can only contains numbers, rather than something which might only contain numbers. If letters are entered, they cannot be converted to numbers, and so the program will crash. This code converts what would have been a program crash into an error message.

You could place this code into a "Start" button, which the user presses after having changed the interval text box to what they desire. This Event must also Enable the Timer (having been disabled via Visual Studio)

Alternatively, you could place it in a TextBox1!Text_Changed event. However, please note carefully that changing a Timer's internal while it is running will cause it to reset, and start from 0 again.

Finally, if you want more control, you can use the DateTime class: DateTime Structure (System)

You can use things like DateTime.Now, DateTime.Subtract(DateTime value), DateTime.Subtract(TimeSpan value), and DateTime.ToString(string format).

If you would like more information, do not hesitate to ask.

Finally, some little tricks relating to this sentence:



Roger ashley said:


> between operations "a" and "b"


If you want this to happen only once (run "b" once), you could set your interval, enable the Timer, but inside the Tick event, you run "b", and then disable the Timer.

You have already seen how to do "b" repeatedly.

However, if you wanted to oscillate between "a" and "b", you could create a global Bool variable, called "IsA", and then inside the Tick event, have something like this:


```
If IsA = True Then
        // Do "a"
Else
        // Do "b"
End If

// Toggle IsA, ready for next time
IsA = Not(IsA)
```
I do not actually use VB.net (primarily being a C# and C/C++ developer). I hope that I haven't accidentally introduced too many non VB.net bits of code here. If some bit of code I have given you doesn't work, feel free to send it back as dead on arrival! 

I hope this helps, and feel free to ask as many more questions as you wish, or on any bits which require further explanation. Good luck!

Richard


----------



## Roger ashley (Apr 14, 2008)

Hi Richard,
I have tried all combinations of the first, even opening up a seperate project window.

IPublic Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
Timer1.Interval = Convert.ToInt32(TextBox1.Text) * 6000
Catch ex As FormatException

End Try
Label1.BackColor = Color.Black
End Sub
End Class 

I ensured the timer was enabled, But I could not make an event occur after the set time delay. I was such a fool to destroy my old PC with XP on it, I would not have all these problems, will press on. I did notice that when I input FormatExecption, it came back with "When"
This needs to happen only once per run session, 
All the best from Sunny Kent.
Roger


----------



## niemiro (Feb 20, 2010)

Roger ashley said:


> Hi Richard,
> I have tried all combinations of the first, even opening up a seperate project window.
> 
> IPublic Class Form1
> ...


Hello again!

Sorry for the delay in responding. This one really has me 

When you say this:

"But I could not make an event occur after the set time delay."

I assume that it does run, but not after the correct delay? The only thing which I can see is you are multiplying by 6000, rather than 60000, which you need to if you wish to program the time in minutes, otherwise the delay will be 10 times shorter than expected.

I really hope this is the answer, because I can't quite think what it might be otherwise. I shall keep thinking! 

All the best from Surrey in drought!

Richard


----------



## Roger ashley (Apr 14, 2008)

Hi Richard,
Overnight I have got a timer to do exactly what I want,
I created a run down timer, When a fiqure is put inot a text box and this matches the fiqure in a label: 
Label1.Text = Val(Label1.Text) +1
Then a second label declares "finished"
I did this in a seperate program to prove without effecting the main program. I can now adjust to suit the need in the main program:smile:
All the best and thanks for all your help.
PyrusBoy


----------



## niemiro (Feb 20, 2010)

Roger ashley said:


> Hi Richard,
> Overnight I have got a timer to do exactly what I want,
> I created a run down timer, When a fiqure is put inot a text box and this matches the fiqure in a label:
> Label1.Text = Val(Label1.Text) +1
> ...


Very well done. I am sorry that I was not of more help here (VB really is more different to C# than I first thought - and they are supposed to be so similar)

If you do have any more questions, I shall try my best...

It has been a pleasure to work with you,

Richard


----------

