Programming Tips and Tricks
Tricks and Fun Stuff with Timers
Click to Run Fun Stuff Program
Click to View Fun Stuff Source Code
| Summing
a List SUM = 0
|
Average
of a List SUM=0 |
| Largest
in a List BIG = first NUM |
Smallest
in a List SMALL = first NUM |
| Counting Even Numbers Uses the "mod" operator CNT = 0 |
Counting Odd Numbers Uses the "mod" operator CNT = 0
|
| Rolling
two Dice R1 = random 1 - 6
|
Dealing
a Card V = "23456789TJQKA" |
| Rounding
a Number to 2 decimals Suppose NUM = 12.9572 NUM = Int (100 * NUM + 0.5) / 100 NUM now equals 12.96 |
Formatting
Output Suppose PRICE = 12.9572 lblAnswer.Caption = Format(PRICE, "$0.00") will output $12.96
|
| Horizontal
Printing To print the following: 1 2 3 4 5 6 7 8 9
10 For X = 1 to 10 |
Time Delay - for delays > 0.01 sec. To cause a delay of S seconds Private Sub Delay(S as Single) |
| Generating a Random List This code fills an
array with 10 random CNT=1 |
Sorting a List of Numbers This code SORTS a
LIST of 10 For I = 1 to 9 |
| Shuffling a List of Numbers This code
shuffles a list by switching For I = 1 to 1000 |
Removing Items from a List This code removes
Bad items and P = 0 |
| Insert an Item in a List This code inserts a
new item at If P <= SIZE Then |
Time Delay 2 - for delays < 0.01 sec. Use in animations where a delay Private Sub Delay(S as Long) |
| Upward motion:
starts at Y=B and goes up to Y=A For Y = B to A step -1 |
Downward
motion: starts at Y=A and falls down to Y=B For Y = A to B |
| Motion in
a straight line: moves a ball from (X1,Y1) to (X2,Y2) in a straight line path DX = X2 - X1 |
Motion in a
Parabolic Path Moves ball from (A,D) and (B,D) in a parabolic path. You must experiment with H to get the proper height. For X = A to B |
Fun Stuff with Timers
| Option Explicit Dim X As Integer Dim SEC As Integer Dim DIS As Integer 'Trick 1 - Switch Graphics 'Uses 2 Image Boxes, one visible and one not 'Reverses the visible property of each box Private Sub cmdTrick1_Click() imgHap.Visible = Not imgHap.Visible imgSad.Visible = Not imgSad.Visible End Sub 'Trick 2 - Blinker 'Uses a Timer Object to Reverse the Visible 'property of an image 'START button enables the Timer Private Sub cmdT2Start_Click() tmrBull.Enabled = True End Sub 'STOP button disables the Timer Private Sub cmdT2Stop_Click() tmrBull.Enabled = False imgBull.Visible = True End Sub 'Timer reverses the Visible state Private Sub tmrBull_Timer() imgBull.Visible = Not imgBull.Visible End Sub 'Trick 3 - Slider 'Uses a timer object to slide a picture Private Sub cmdT3Start_Click() X = 0 tmrCam.Enabled = True End Sub 'Timer moves the Camera 50 units and returns it 'to the start after going 1000 units Private Sub tmrCam_Timer() X = X + 50 picCam.Left = picCam.Left + 50 If X = 1000 Then X = 0 picCam.Left = picCam.Left - 1000 End If End Sub 'Stop button disables the timer and returns 'the picture to the start Private Sub cmdT3Stop_Click() tmrCam.Enabled = False picCam.Left = picCam.Left - X End Sub 'Trick 4 - Flasher 'Start button enables a timer which switches 'the FillStyles of two shapes Private Sub cmdT4Start_Click() tmrFlash.Enabled = True End Sub 'Stop button disables the timer Private Sub cmdT4Stop_Click() tmrFlash.Enabled = False End Sub 'Timer changes the fillstyles of the red 'and green circles Private Sub tmrFlash_Timer() If shpRed.FillStyle = 0 Then shpRed.FillStyle = 1 shpGrn.FillStyle = 0 Else shpRed.FillStyle = 0 shpGrn.FillStyle = 1 End If Beep End Sub 'Trick 5 - Countdown 'Enables a timer which counts down 10 seconds Private Sub cmdT5Start_Click() cmdT5Start.Enabled = False SEC = 10 lblClock = SEC tmrClock.Enabled = True End Sub 'Timer decreases count and resets at 0 Private Sub tmrClock_Timer() SEC = SEC - 1 lblClock = SEC If SEC = 0 Then tmrClock.Enabled = False Beep cmdT5Start.Enabled = True End If End Sub 'Trick 6 - Rocket 'Enables a timer to move the rocket Private Sub cmdT6Start_Click() tmrRocket.Enabled = True End Sub 'Timer moves rocket upward in the image box 'until it reaches the top. It then resets the 'position of the rocket. Private Sub tmrRocket_Timer() DIS = DIS + 50 picRocket.Top = picRocket.Top - 50 If DIS = 2100 Then DIS = 0 picRocket.Top = picRocket.Top + 2100 tmrRocket.Enabled = False End If End Sub Private Sub cmdExit_Click() Unload Me End End Sub <Top> |
This is the source code for the Timer Tricks Click [Here] to run the program Trick 1 uses 2 image boxes placed on top of each other. Make sure you set the visible property of the sad face to FALSE and the happy face to TRUE during design.
Trick 2 uses a Timer object with interval 200. It must be disabled during design. When the Start button is clicked the Timer is enabled and the bullseye will flash off and on until you click the STOP button.
Trick 3 is fairly complex. A Timer is used to slide a picture box 1000 units. The timer has an interval of 100 and is disabled during design. It is activated when the Start button is clicked. Once the picture has moved 1000 units it is sent back to the start position and moves again.
See if you can figure out how the remaining tricks work.
|