VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Time Bomb



This program simulates a a time bomb which is ticking away and will explode within 60 seconds. To defuse the bomb, you need to enter the correct password code.

To make the program simpler, the user only needs to key-in a three-digit password which is fixed by the programmer using the declaration

   Const code As Integer = 398

To design an attractive interface, you can use the image of a bomb and an image of an explosion. Make the explosion image invisible at startup as it will show up only if the user fail to defuse the bomb. You also need to insert two command buttons, label one of them as Confirm and the other one as Reset. Besides, insert three text boxes to be use as password panel. Insert a timer control and program it so that it can start the countdown. You can set the timer's interval to 1000 so that each countdown is one second or you can set shorter interval. 

The sub procedure for the Timer to start countdown is as follow:

Private Sub Timer1_Timer()
'To display countdown time. if it is equal to 0, it sets off the destruction procedure
countdown = 60 - x
If countdown <= 60 And countdown > -1 Then
Lbl_Timer.Caption = Str$(countdown)
x = x + 1
ElseIf countdown < 0 Then
Timer1.Enabled = False
destruction
End If
End Sub
 

You can also insert a Multimedia Control to play the explosion sound. Note that the Multimedia Control does not appear in the default startup IDE, you need to add it from the component. To do that, go to the menu and click project, select components from the drop-down menu. Choose Microsoft Multimedia control 6.0 from the list of available controls, as shown in the diagram below. After that the multimedia control will appear in the toolbox on the left. You must also download an explosion wav sound file from the Internet.

The code to play the sound file is as follows:

MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "MMControl1.Notify = False"
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB program\audio\bomb.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"

 


The Interface

 

Bomb Sucessfully Deactivated

Deactivation Failed


The Code


'Declaring variables and constants

Dim countdown As Integer
Dim x As Integer
Const code As Integer = 398

Private Sub Form_Load()
Timer1.Enabled = True
MMControl1.Visible = False
'To generate a three-digit random password
Randomize Timer
code = Int(Rnd * 1000)
Lbl_Status.Visible = True
Lbl_Status.Caption = Str$(code)

End Sub

Private Sub Lbl_Confirm_Click()
'To compare the password entered to the generated password

y = Trim(Text1.Text) + Trim(Text2.Text) + Trim(Text3.Text)

If y = code Then

Lbl_Status.Visible = True
Lbl_Status.Caption = "Deactivation Sucessful!"

Timer1.Enabled = False
Else
Lbl_Status.Visible = True
Lbl_Status.Caption = "Wrong Password! Reset and Try again!"

End If
End Sub
Private Sub Lbl_Reset_Click()
Lbl_Status.Visible = False

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus

End Sub

Private Sub Text1_Change()
If Text1.Text <> "" Then
Text2.SetFocus
End If

End Sub

Private Sub Text2_Change()
If Text2.Text <> "" Then
Text3.SetFocus
End If

End Sub

Private Sub Timer1_Timer()
'To display countdown time. if it is equal to 0, it sets off the destruction procedure

countdown = 60 - x
If countdown <= 60 And countdown > -1 Then
Lbl_Timer.Caption = Str$(countdown)
x = x + 1

ElseIf countdown < 0 Then

Timer1.Enabled = False

destruction

End If

End Sub

Sub destruction( )

Lbl_Status.Visible = True
Lbl_Status.Caption = "Deactivation Fail!"
Image2.Visible = False
Text1.Visible = False
Text2.Visible = False
Text3.Visible = False
Lbl_Timer.Visible = False
Label2.Visible = False
Shape1.Visible = False
Image1.Visible = True
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "MMControl1.Notify = False"
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB program\audio\bomb.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"

End Sub


 


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy