Print Page | Close Window

Back to Basics 1

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=23
Printed Date: 20 December 2006 at 6:02pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Back to Basics 1
Posted By: Sync
Subject: Back to Basics 1
Date Posted: 07 February 2006 at 5:03pm
Originally Posted By Zwabbe Wolfy

I couldnt find an approraite forum for this so i guess here will do for now. Im gonna be making a few tutorials that show the very basic functions of Visual Basic to show the newbies through the door.. Probably wont work but meh oh well...



A Basic Stop,Start,Reset Timer

To learn the basic Functions of a Timer, Simple addition, Command buttons, And Changing a items properties.

Firstly you will need to lay out a form like so:


And label the items as so:

  • Cstart - The Start button

  • Cstop - The Stop button

  • Creset - The Reset button

  • Lhour - The Label for the hours

  • Lmilli - The Label for the Milliseconds



With the Minute and Seconds we need to make two labels for each. One Will take up the space of 2 zeros and the other will be placed over the first zero of the other label

This is used so we can display 2 digits in the minutes and seconds at all times.

We will be calling these labels lseccound, lsecdisplay, lmincount, lmindisplay. The Display is the 1 digit 0 and the count is the 2 digits.

The Timer will remain Timer1 and the labels used for the : in between numbers can remain as label1..etc..etc

Now to put in the code for the timer!

In Cstop you want your code to look like so
Private Sub Cstop_Click()
    Timer1.Enabled = False
End Sub

What this means is that when the user clicks on the Stop button the Timer that is adding the milliseconds onto the count will stop. Then the Main timer will stop leaving the current time it was on displayed.

InCstart you want this code
Private Sub Cstart_Click()
    Timer1.Enabled = True
End Sub

Basically the opposite of Cstop this turns the timer back on so it can continue adding milliseconds to the count.

And Creset will be like so
Private Sub Creset_Click()
    Lhour.Caption = "00"
    lmincount.Caption = 0
    lseccount.Caption = 0
    Lmilli.Caption = "00"
    lmindisplay.Visible = True
    lsecdisplay.Visible = True
End Sub

So what is happening here? Firstly the Labels on the timer are put back to 0 values. The Hour and Milli values use "00" Because they do not have the display 0's that minutes and seconds have. Then the display 0's are also made visible again. So in the end your timer will switch back to 00:00:00:00

The Running of the Timer
So how does this timer work now that we have the layout set up. All of this following code will go under Timer1. First Timer1 should be set to Interval's of 1 and make its enabled setting false. This will make the timer tick over every millisecond which is required to run a timer. And will also make it that the Start button needs to be pressed before the timer will run.

If Lmilli.Caption = 99 Then
        Lmilli.Caption = "00"
        lseccount.Caption = lseccount.Caption + 1
    End If


This is the first part of the timer. What this section does is check when our milli seconds reach 99. As there is 100 milli seconds in 1 second, when we reach 99 the program does two basic things. It clicks the milli seconds back to 00. And it adds 1 second to the SecondNow nt. Notice how to add one seconds we need to make the lseccount.caption equal itself + 1. There we go basic addition use.


Now we move onto seconds.
If lseccount.Caption = 9 Then
        lsecdisplay.Visible = False
        lseccount.Caption = lseccount.Caption + 1
    End If


Now what is happening here? what happens on this stage is that it checks to see if the seconds have reached 9 yet. As when seconds reach 9 they are then followed by 10. So we no longer need the extra 0 to make double digits. So here what happens is the lsecdisplay is made invisible. (Visible = false) and another 1 is added to the seconds to make it double digits.

If lseccount.Caption = 59 Then
        lseccount.Caption = 0
        lsecdisplay.Visible = True
        lmincount.Caption = lmincount.Caption + 1
    End If


So then seconds get to 59.. (60 seconds = 1 min).. First the seconds are reset back to cound 0. And the extra 0 is placed back over the space to make two 0's shown. Then we at the first minute into the timer. by adding 1 to the minutes. So on the balll rolls

Minutes work exactly the same It goes up to 9, takes away the extra 0. When it reaches 59 it goes back to 0 and ticks up the hours.

If lmincount.Caption = 9 Then
        lmindisplay.Visible = False
        lmincount.Caption = lmincount.Caption + 1
    End If
   
   If lmincount.Caption = 59 Then
       lmincount.Caption = 0
       lmindisplay.Visible = True
        Lhour.Caption = Lhour.Caption + 1
    End If


The Next peice of code you dont have to have, But its just a tipper that when the timer reaches 99 hours it will switch back to the beggining. Whover has this timer running for 99 hours i dont know. You can always modify the timer if its being used for something like a Server up time count. to just run into 99999999..etc..etc hours. by removing this tripper and by expanding the forms space you you can see all the hours.

If Lhour.Caption = 99 Then
        Lhour.Caption = 0
        lmincount.Caption = 0
        lseccount.Caption = 0
        Lmilli.Caption = "00"
        lmindisplay.Visible = True
        lsecdisplay.Visible = True
        Timer1.Enabled = False
    End If

As you can see just like the rest button everything goes back to 0 settings but at the same time the timer switches off waiting for another round. (This doesnt really have to be put in.. As like all visual basic its what you want to create!

The last essential bit of code is the must have peice!
Lmilli.Caption = Lmilli.Caption + 1


As the timer ticks over every milli second it also needs to add the millisecond to the count.




In the end your final complete timer code should be:

Private Sub Creset_Click()
    Lhour.Caption = "00"
    lmincount.Caption = 0
    lseccount.Caption = 0
    Lmilli.Caption = "00"
    lmindisplay.Visible = True
    lsecdisplay.Visible = True
End Sub

Private Sub Cstart_Click()
    Timer1.Enabled = True
End Sub

Private Sub Cstop_Click()
    Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
    If Lmilli.Caption = 99 Then
        Lmilli.Caption = "00"
        lseccount.Caption = lseccount.Caption + 1
    End If
   
    If lseccount.Caption = 9 Then
        lsecdisplay.Visible = False
        lseccount.Caption = lseccount.Caption + 1
    End If
   
    If lseccount.Caption = 59 Then
        lseccount.Caption = 0
        lsecdisplay.Visible = True
        lmincount.Caption = lmincount.Caption + 1
    End If
   
    If lmincount.Caption = 9 Then
        lmindisplay.Visible = False
        lmincount.Caption = lmincount.Caption + 1
    End If
   
   If lmincount.Caption = 59 Then
       lmincount.Caption = 0
       lmindisplay.Visible = True
        Lhour.Caption = Lhour.Caption + 1
    End If
   
  If Lhour.Caption = 99 Then
        Lhour.Caption = 0
        lmincount.Caption = 0
        lseccount.Caption = 0
        Lmilli.Caption = "00"
        lmindisplay.Visible = True
        lsecdisplay.Visible = True
        Timer1.Enabled = False
    End If
   
    Lmilli.Caption = Lmilli.Caption + 1

End Sub





So why make a timer??? Well in a online game sense it has many features. Things you might use it for could be a server uptime counter. That activates when the server is started and when ever i client goes to log in you could show the server uptime to the player just to prove you have a stable uptime.. This kind of timer would probably also go into days, weeks and maybe even months.

Or maybe you want a timed quest. You would make it a countdown timer! Which instead of working from 0 up it works from the time down (lmincount.caption = lmincount.captoin - 1)

Or player play time counter. Each time a player logs in it counts there online time and when they log out it may add it to there total game time.

One other use could be an inactivity timer. If a player has not moved for 10mins they could be logged out.

Thats just a few of the uses of a timer.











Replies:
Posted By: Sync
Date Posted: 07 February 2006 at 5:04pm
Images are not showing, will approve when links are fixed. Otherwise, this tutorial rules 



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info