locked
Timer Tick Event RRS feed

  • Question

  • Hi all 

    I used a timer in my user control now when Timer_Tick event is fired I want to raise an event like Ticked 

    I created 

    public delegate sub myDel()

    public event myevnt as myDel

     

    in Timer.Tick 

    I used raiseEvent myevnt 

    I also raised the event from a button click

     

    This event is handled in a windows form where the control is used 

    My problem is event is firing when the button is clicked but 

    not firing from the timer.tick

     

    Is there any problem from Timer.Tick 

     

    pls suggest me

     

    Thanks

     

     


    bahushekh
    Thursday, February 10, 2011 11:44 AM

Answers

  • I tested a sample and it works perfectly. So possibly you are missing something or doing something in the wrong way.

    This is what I did:

    1. Added a UserControl to my project and named it TestTimerEvent

    2. Added a timer control (Timer1) to it and the following code:

    Public Class TestTimerEvent
    
      Public Delegate Sub myDel()
      Public Event myevnt As myDel
    
      Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RaiseEvent myevnt()
      End Sub
    
      Private Sub TestTimerEvent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer1.Enabled = Not DesignMode
      End Sub
    End Class
    
    

    3. Set the Timer1.Interval property to 1000 in properties window. (you can do it in code also if you want to, or whatever way it is appropriate)

    4. Compiled the application. Then dragged a Label control and TestTimerEvent control from the toolbox to my form.

    5. Added the following code to my form:

      Private Sub TestTimerEvent1_myevnt() Handles TestTimerEvent1.myevnt
        Static i As Integer
        i += 1
        Label1.Text = i.ToString
      End Sub
    
    6. Run application and see the number ticking in the Label.

     

     

     

     


    Pradeep, Microsoft MVP (Visual Basic)
    http://pradeep1210.wordpress.com
    • Proposed as answer by SJWhiteley Thursday, February 10, 2011 3:24 PM
    • Marked as answer by bahushekh Thursday, February 10, 2011 7:01 PM
    Thursday, February 10, 2011 12:32 PM

All replies

  • Set timer.Enabled = true

    Unless you set the enabled property to true, the tick event is not fired


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Thursday, February 10, 2011 12:07 PM
  • private void button1_click(object, sender, EventArgs e)
    {
       Timer timer1 = new Timer();
       timer1.Tick += new EventHandler(timer1_Tick)

       (You can also add the properties of the timer tick event, such as the interval, and so forth).
    }

    private void timer1_Tick(object, sender, EventArgs e)
    {
       (You can now add the code you would like here)
    }

    this will help you out i think.....


    • Proposed as answer by Civic1986 Thursday, February 10, 2011 12:18 PM
    • Unproposed as answer by SJWhiteley Thursday, February 10, 2011 3:23 PM
    Thursday, February 10, 2011 12:18 PM
  • I tested a sample and it works perfectly. So possibly you are missing something or doing something in the wrong way.

    This is what I did:

    1. Added a UserControl to my project and named it TestTimerEvent

    2. Added a timer control (Timer1) to it and the following code:

    Public Class TestTimerEvent
    
      Public Delegate Sub myDel()
      Public Event myevnt As myDel
    
      Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RaiseEvent myevnt()
      End Sub
    
      Private Sub TestTimerEvent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer1.Enabled = Not DesignMode
      End Sub
    End Class
    
    

    3. Set the Timer1.Interval property to 1000 in properties window. (you can do it in code also if you want to, or whatever way it is appropriate)

    4. Compiled the application. Then dragged a Label control and TestTimerEvent control from the toolbox to my form.

    5. Added the following code to my form:

      Private Sub TestTimerEvent1_myevnt() Handles TestTimerEvent1.myevnt
        Static i As Integer
        i += 1
        Label1.Text = i.ToString
      End Sub
    
    6. Run application and see the number ticking in the Label.

     

     

     

     


    Pradeep, Microsoft MVP (Visual Basic)
    http://pradeep1210.wordpress.com
    • Proposed as answer by SJWhiteley Thursday, February 10, 2011 3:24 PM
    • Marked as answer by bahushekh Thursday, February 10, 2011 7:01 PM
    Thursday, February 10, 2011 12:32 PM
  • private void  button1_click(object , sender, EventArgs  e )
    {
       Timer timer1 = new Timer();
       timer1.Tick += new EventHandler(timer1_Tick)

       (You can also add the properties of the timer tick event, such as the interval, and so forth).
    }

    private void  timer1_Tick(object , sender, EventArgs  e )
    {
       (You can now add the code you would like here)
    }

    this will help you out i think.....


    Hi Civic,

    This is a Visual Basic forum, so please post responses accordingly.

    bahushekh,

    Please check your Timer.Interval property and try using

    Timer1.Start()
    

    OR

    Timer1.Enabled = True
    
    However I would suggest the former as Timer1.Start strictly starts the timer (opposite of Timer1.Stop), whereas the enabled property can be used in If...Then statements, as it gets or returns whether the timer is started.

    Hope this helps,

    Samuel


    Test
    Thursday, February 10, 2011 12:59 PM