TimerControl_Interval Property

 

  

  The following code is a simple elapsed time counter.

  I want to change the Timer_Control.Interval property during

  run t ime during a Button_Click event handler.

  Can this be done

  I also would like to initialize the counter when the VB

  program starts. I don't know where to put any initialization code.

 

 

 

Public Class Form1

   

    Private MyStartTime As DateTime = Now()

    Private MyEndTime As DateTime = Now()

    Private MyTimeSpan As TimeSpan

    I would like to add code here to initialize timer.

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e

    As    System.EventArgs) Handles Button1.Click

 

   I would like to add code here to change Timer_Control Interval or Enabled properties.

 End Sub

 

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

 

        MyStartTime = MyEndTime

        MyEndTime = Now()

        MyTimeSpan = MyEndTime.Subtract(MyStartTime)

        Label1.Text = MyStartTime

        Label2.Text = MyEndTime

        Label3.Text = MyTimeSpan.ToString

    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

    I would like to add code here to initilize timer.

 

    End Sub

End Class




Answer this question

TimerControl_Interval Property

  • SamScratch

    The name of the sub is unimportant its whats after the handles keyword that is important.

    So you could for example call you sub XYZanything

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

    MyStartTime = MyEndTime

    MyEndTime = Now()

    MyTimeSpan = MyEndTime.Subtract(MyStartTime)

    Label1.Text = MyStartTime

    Label2.Text = MyEndTime

    Label3.Text = MyTimeSpan.ToString

    End Sub

    The handles timer1.tick is what hooks this sub up to the Timer1 controls tick event.

    The default names that VB tries to give controls is based upon the control and event but if you create a sub and then change the control name. You'll notice that the handles will reflect the new name of the control but the sub name wont change and therefore the example above where the sub is called XYZanything.


  • emates

    I found my problem with your help.

    If you look at the event handler for the timer_control , the name of the sub is

    Timer1_tick which VB assigned . I thought the name for the timer_control was the name of the sub ; however,the name for the timer is Timer1.

    Do you know what the _tick signifies

    I thought about the _tick. It's the name of the event.

     

    Thanks

     



  • Lee.NET

    Sure you can chnage the timer interval property at run time.

    simply put

    Timer1.Interval = <Value in MS>

     

    For a counter you may define a class level variable

    private Counter as long = 0

     

     

    or you could define a class level variable

    Private Counter as long

    and in the form load set its intial value

    Counter = 0

     

     

    Then in the timer tick event add something like

     

    counter +=1  

     

    this will increment the counter every time the the tick event is run. 

     

    Then the elapsed time will be counter * timer1.interval

     

     

     

     


  • EvanstonIl

    I am just begining to learn VB express

    Thanks for the reply which makes sense.



  • TimerControl_Interval Property