General Development

vb question.. – bennythebear

bennythebear

Member

Posts: 1225
From: kentucky,usa
Registered: 12-13-2003
ok, i want to know who the timer control works in vb.net. i've done some googling and found samples of it being used, but they never fully explained it, so all i could possibly do is copy and paste their code, etc. etc... but can someone explain to be how to set the intervals, and then how to use it, or "call" it. i don't know if any of you ever use vb, but any help would be appreciated.

------------------
proverbs 17:28
Even a fool, when he holdeth his peace, is counted wise: and he that shutteth his lips is esteemed a man of understanding.

proverbs 25:7
open rebuke is better than secret love.

www.gfa.org - Gospel for Asia

www.persecution.com - Voice of the Martyrs

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
i have like 5 years experience with visual basic 6, lol.
the timers probably work very similar so i might be able to help.
in vb6 there was a timer function that you put the code that you want to be executed when the timer goes off... anyone else want to jump in? lol i don't know the .NET IDE that well
HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
Yeah, timers in VB.Net work almost exactly like they did in VB6.

A timer has 2 main properties that you're interested in, and 1 event.

The first property that you already mentioned is "Interval" -- this is an integer that specifies how long of a delay it will take between ticks.

The second property is "Enabled" -- it's a boolean (true/false) that lets you know if the timer is running or not. Think of it as an on/off switch.

The event is OnTick (or something close to it). It's just like the OnClicked event for buttons, or the OnLoad event for forms, except that it fires every so many milliseconds if the timer is enabled.


Sorry it's not a very thorough explanation, but I don't have time to write sample code right now.

--clint

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
there we go i thought it was something like that but i don't have vb6 or vb.net on my laptop to write it up
bennythebear

Member

Posts: 1225
From: kentucky,usa
Registered: 12-13-2003
ok. thanks. i'll play around with it a bit more to see if i can get it working for me. i'll post my results.

------------------
proverbs 17:28
Even a fool, when he holdeth his peace, is counted wise: and he that shutteth his lips is esteemed a man of understanding.

proverbs 25:7
open rebuke is better than secret love.

www.gfa.org - Gospel for Asia

www.persecution.com - Voice of the Martyrs

bennythebear

Member

Posts: 1225
From: kentucky,usa
Registered: 12-13-2003
ok. do i set the interval in the control i'm trying to user the timer in, or in the timer_click event? also where do i enable it. when i try to call the timer_click event it says something's not declared, or you can't call it directly use the "RaiseEvent" thing to use it. maybe i can google come samples and look at how they did it and now maybe it might make sense to me. i'm just glad i'm trying to learn any of the c varients out there...i see all those brackets and i just close out the ide .

------------------
proverbs 17:28
Even a fool, when he holdeth his peace, is counted wise: and he that shutteth his lips is esteemed a man of understanding.

proverbs 25:7
open rebuke is better than secret love.

www.gfa.org - Gospel for Asia

www.persecution.com - Voice of the Martyrs

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
The interval is set in the timer control's property window. It will say "Interval" and you need to enter an integer value. This is the frequency at which the timer will be "fired" in milliseconds.

The Enabled value is set in the timers' control property window as well. Make sure this is set to true.

Double-Click the Timer Control, and that will bring you to the function (code). This function is fired at every X milliseconds (where X is the interval).

The timer control will be located in the section below the form, above the output window. Where other controls such as menus, save/open file dialogs, etc...

------------------
"But it is God who judges: He brings one down, he exalts another." - Psalm 75:7

bennythebear

Member

Posts: 1225
From: kentucky,usa
Registered: 12-13-2003
ok, i used msdn's code and changed it to do what i was trying to do, which is make a counter. basically change the labels text to the labels text + 1 every second. it keeps adding, but it doesn't do it every second, and the interval is at 1000. well here's the code:
<BLOCKQUOTE><table width=80% border=0 bgcolor="#FFFFF" CELLPADDING="2" CELLSPACING="2"><TR><TD><font size="3" face="Courier" color="#000000"><pre> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
' Set the caption to the current time.
Label1.Text = Label1.Text + 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub
</pre></font></TD></TR></TABLE></BLOCKQUOTE>

i think it was the "initialize timer" sub that i was missing. does anyone know why it wouldn't add +1 every second though?

------------------
proverbs 17:28
Even a fool, when he holdeth his peace, is counted wise: and he that shutteth his lips is esteemed a man of understanding.

proverbs 25:7
open rebuke is better than secret love.

www.gfa.org - Gospel for Asia

www.persecution.com - Voice of the Martyrs

[This message has been edited by bennythebear (edited August 06, 2006).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
Just a theory but, It could be that it's not really that "precise" in calculating the tick. I know in C/C++ there are possible errors with timing using different methods, and so try to use the QueryPerformance(Counter/Frequency) rather then timeGetTime, or other timing methods available.

You should google timing methods for vb, you should be able to find someone who's done some profiling of some different methods and look for the most precise.

------------------
"But it is God who judges: He brings one down, he exalts another." - Psalm 75:7

bennythebear

Member

Posts: 1225
From: kentucky,usa
Registered: 12-13-2003
thanks for the advice.

------------------
proverbs 17:28
Even a fool, when he holdeth his peace, is counted wise: and he that shutteth his lips is esteemed a man of understanding.

proverbs 25:7
open rebuke is better than secret love.

www.gfa.org - Gospel for Asia

www.persecution.com - Voice of the Martyrs

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
Timers are acceptable for situations that don't need to be precise. Frame-rate based games will run choppy if they use timers, but if you're just trying to animate a progress bar or do some other dinky task, then timers work very well. Timers are also simple enough that you can learn how to easily make games by using them, but they're not a good long term solution. My first game in VB was made using timers and moving picture boxes around the screen. It worked, but didn't work well, and it helped me learn a lot about performance and how to improve it.

--clint

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
do NOT use timers for anything important, lol. for that you need to use a main loop and some timing code (vb gives you some easy ways to grab the time).
on a side note, vb is a blast to code. its easy, fast and you can do some nifty stuff with it.