creating a visible timer in visual basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • terry guthrie
    New Member
    • Sep 2007
    • 4

    creating a visible timer in visual basic

    I need some help with visual basic code, I need to create a timer that will be visible while playing a game, i know there is a timer in the tools that you pull over to the project and is hidden, and then i create a label on the page itself, the purpose is to show in seconds how long it takes to complete a game. what is the code that will link the timer with label so when i start my game you can see how many seconds it is taking.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by terry guthrie
    I need some help with visual basic code, I need to create a timer that will be visible while playing a game, i know there is a timer in the tools that you pull over to the project and is hidden, and then i create a label on the page itself, the purpose is to show in seconds how long it takes to complete a game. what is the code that will link the timer with label so when i start my game you can see how many seconds it is taking.
    (Note, I'm assuming VB6 here...)

    One simple way would to store the value returned by the Timer() function (ignore the name, it's just a coincidence) when you start, then in the Timer1_Timer event, set the label caption to the difference between that original value and the current time.

    For example, create a new form, add a timer (with Interval = 1000) and a label, and paste this into the code window...
    [CODE=vb]Option Explicit
    DefLng A-Z

    Private StartTime As Long

    Private Sub Form_Load()
    StartTime = Timer
    End Sub

    Private Sub Timer1_Timer()
    Label1.Caption = CLng(Timer - StartTime)
    End Sub[/CODE]

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      Hi, see if this helps at all...

      Display Time

      [CODE=vbnet]Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick
      TimeToolStripMe nuItem.Text = Now.ToLongTimeS tring() 'Display time in menu strip
      End Sub[/CODE]
      Last edited by Killer42; Sep 14 '07, 01:49 PM. Reason: Added [CODE=vbnet] tag

      Comment

      Working...