Newbie trying to understand the use of a Timer in Visual Studio VB

Hi

I have copied the example code from the help files.

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref17/html/T_System_Windows_Forms_Timer.htm

but cannot figure out how to use it.

I tried a whole bunch of things involving stuff like:

Dim fred As New Timer

fred.Enabled = True

fred.Start()

expecting the included 5000 ms timer to start and count down to zero then show the message as per the example code - but noting happened.

How do I use a Timer

 




Answer this question

Newbie trying to understand the use of a Timer in Visual Studio VB

  • Nagaraju Palla MSFT

    The one thing that is missing your code is the event that is to be called when the timer reaches zero.

    The easiest way to see what you need to do is

    1) Create a new app with a single form

    2) Drag a timer onto the form from the components group in the toolbox. It will appear at the bottom of the form.

    3) Highlight the timer and look at its properties and click on the events panel.

    4) Double click on the tick event. The following code - which you have omitted - will be created for you:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub

    Forgive the continuing formating of this response - I hate this interface:-(

    What I would now do is add the following to the automatically generated code

    Try

    Dim timer As Timer = CType(sender, Timer)

    timer.Enabled = False ' do this or you can end up with a waterfall of windows if you don't get the first closed fast enough

    MsgBox("The timer ticked")

    timer.Enabled = True 'do this or it won't tick again

    Catch ex As Exception

    ' your preferred event error handling goes here for now use

    MsgBox("oops" & vbCrLf & ex.Message)

    End Try

    Now if you want to futz with your already existing code and way of doing business

    1. Add the event code as shown above and put what you want in it. Omitt the portion that says Handles Timer1.tick. It won't compile with what you are doing.

    2. Modify your existing code by adding the following line after your new timer line:

    AddHandler Timer1.Tick, AddressOf Me.Timer1_Tick

    I couldn't immediately find the example you cited - who reads the book any way - so I don't know if I have found the actual source of your problem - viz the missing handler and addhandler lines. But I know this works cause i just did it:-)))

    HTH

    Regards,

    Al Christoph
    Senior Consultant and Owner
    Three Bears Software, LLC
    just right software @ just right prices @ 3bears.biz
    A Microsoft Certified Partner (ISV)
    Working on utilities for Windows Mail for Vista.


  • shav

    "I believe that I understand it sufficiently well to enable me to do it at run time now."

    This is a huge gain! It's the beginning of an awaking.

    I'm really glad this forum means something to you.



  • Careltje

  • LOD

    So now you have two solutions here.

    One involves dropping a control on the form at design time and setting its properties and tick event

    The other involves creating the time control at runtime in which case you need to hook up the elapsed event to achieve the same result.


  • B. Clubb

    Thanks to all, I now have my Timer up and running - the main fault was that I hadn't realised that I needed to drag a Timer control onto my form. It doesn't want to work like that

  • Colby75

    Public Class Tests

    Public function Add (byval a as integer, byval B as integer) as integer

    return A + B

    End function

    End Class

    'In Main Code....

    dim test as new tests

    Dim sum as integer = test.Add(2,3)

    And there you are !



  • Brian Morgan

    Once I realised that I need to drag the Tiner control onto the form, everything fell into place for me. I also now see the method of creating the Timer at run time.

    During my initial failed attempts, I believe I was mixing the two methods up together.

    I should have known better, since I have created other control types at run time, including their event handlers, and I just plain didn't see the connection when I tried to do the Timer.

    Once I was able to set one up via the conrol on the form, I then saw the pattern right away.

    Thanks once again to you all.



  • BPB

    OK dokey, I have now split off some of my code to a new module and all still working.

    Why not

    Dim sum as integer = tests.Add(2,3)

    without the

    dim test as new tests



  • Mario Ruiz

     

    Leshay,

    You keep saying you have to drag a timer. You don't. Actually I think most of the pros write their own inline rather than drag them. The code is actually more portable that way.



  • Nicolas S. Nielsen

    My days of being under supervision are over.

    Although, having said that, I do know the importance of kepping things in an easily managed set of smaller self contained modules or 'pebbles'.

    I can see the overall advantages in that approach, but for me, I need to get as much of the code working as possible - to be able to see where the rock can be broken to form pebbles.

    I did see that starting a new class leant itself to a seperate code module, but until I manage to figure out the means of inter module coding, I just had to glue the pebbles back into one giant rock

    I will, no doubt, be back in the forum asking all sorts of newbie questions in the near future.



  • Gozzeh

    I'm not sure this example but I'll point you to another tutorial

    http://abstractvb.com/code.asp F=26&P=1&A=1032

    The key points to remember are that you

    1. set a interval on the time in Milliseconds
    2. start it
    3. And hook up and event handler to do something every time the tick event is raised.

    1 and 2 are pretty self explanatory and in this case it sets the interval on the when creating a new instance of the timer but you could set the interval property separately.

    but 3 involves two steps in this example

    AddHandler t.Elapsed, AddressOf TimerFired

    This is setting up the event so that when the timer interval has elasped it is going to call the TimerFired method.

    Obviously you also need to define a TimerFired method which I did as

    Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) MsgBox("Signal Time = " & e.SignalTime.ToString)
    End Sub


    Now if you didnt set up the step 3 then you timer is going to work but it isnt going to do anything each time the interval elapses and therefore you dont see anything.

    If you just drop a timer control on the form its real simple code something in the tick event and this will occur every interval as the event handler is hooked up using the handles keyword.


  • Hector Urizar

    ReneeC wrote:

    Leshay,

    You keep saying you have to drag a timer. You don't. Actually I think most of the pros write their own inline rather than drag them. The code is actually more portable that way.

    Yes, I am becoming aware of the point you make, but in general, the Form control method enabled me to make sence of the procedure. I believe that I understand it sufficiently well to enable me to do it at run time now.

    I have had the Visual Studio Express for less than a week and am making good progress, in no small part due to the forum members.



  • haraldhw

    Well, I am managing to understand certain aspects of things as I progress, but unfortunately many other aspects do cause me plenty ot problems.

    Just when I feel I can manage one thing, another comes along to bring me back down to earth

    For example. I have kept all my code in one class module as I couldn't figure out how to relate to items etc in another class module. So, just to make some headway, I just threw it all into the one default code module.

    There is so much to get a handle on! At my age, very slow but sure ......



  • Mike111

    As for THAT, Visual Studio handles all the linkages. Just add another form/module/whatever to the project.

    There's nothing wrong with grouping all related programming into one class/module. There's no need to reduce everything to pebbles if you can lift 1-pound rocks. Unless, of course, you have a supervisor breathing down your neck who demands pebbles...


  • Newbie trying to understand the use of a Timer in Visual Studio VB