You are on page 1of 47

Presidency College

Department of Computer Applications


Visual Programming BCA 403P

Student Name:

Sem & Section:

VISUAL
PROGRAMMING LAB
MANUAL
Program List:
Part -B Programs

11. Write a VB program to design a simple calculator to perform addition, subtraction,


multiplication and division (control Arrays)

12. VB Application to change the color of the text in a text box use scroll bar for the selection of
colors

13. VB Application to develop a simple AVI Player

14.VB Application to write into a Binary File and To Read from a binary File

15. VB program to Encrypt and Decrypt a string.(Use Rnd() to generate the Encryption and
Decryption keys.)
16. Write a VB program to Animate an Image
17.VB program to validate Fractional Number
18.VB program to show SCROLLBAR
19.VB program to show SCREEN SAVER
20.VB program to show SPLASH SCREEN

BCA403P - VISUAL PROGRAMMING LAB MANUAL


Part A -1. Student Details

Private Sub cmdresult_Click()


If txtstudentname.Text = "" Then
MsgBox "Please enter student name: ", vbYesNoCancel
Exit Sub
End If
If txttotalmarks.Text = "" Then
MsgBox "Please enter total marks: "
Exit Sub
End If
If Val(txttotalmarks.Text) > 800 Then
MsgBox "Total marks exceed maximum limit."
Exit Sub
End If
If Val(txttotalmarks.Text) = 0 Then
txtpercentage.Text = "FAIL"
txtdivision.Text = "NA"
Exit Sub
End If

txtpercentage.Text = (Val(txttotalmarks.Text) * 100) / 800


If Val(txtpercentage.Text) >= 75 Then
txtdivision.Text = "A+"
ElseIf Val(txtpercentage.Text) >= 60 And Val(txtpercentage.Text) < 75 Then
txtdivision.Text = "A"
ElseIf Val(txtpercentage.Text) > 40 And Val(txtpercentage.Text) < 60 Then
txtdivision.Text = "B"
Else
txtdivision.Text = "FAIL"
End If
End Sub

Private Sub txtstudentname_KeyPress(KeyAscii As Integer)


If Not Chr(KeyAscii) Like "[A-Z,., ,a-z]" And Not KeyAscii = 8 Then
KeyAscii = 0
End If
End Sub

Private Sub txttotalmarks_GotFocus()


MsgBox "Please enter only numerals"
End Sub

Private Sub txttotalmarks_KeyPress(KeyAscii As Integer)


If Not Chr(KeyAscii) Like "[0-9]" And Not KeyAscii = 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txttotalmarks_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
Case Else
KeyAscii = 0
MsgBox "ENTER NUMBERS ONLY"
End Select

End Sub
Part A 2. MDI Form

Private Sub mnuback_Click()


CommonDialog1.ShowColor
frmformat.txtdisplay.BackColor = CommonDialog1.Color
End Sub

Private Sub mnubold_Click()


frmformat.txtdisplay.FontBold = True
End Sub

Private Sub mnuexit_Click()


End
End Sub

Private Sub mnufore_Click()


CommonDialog1.ShowColor
frmformat.txtdisplay.ForeColor = CommonDialog1.Color
End Sub

Private Sub mnuitalics_Click()


frmformat.txtdisplay.FontItalic = True
End Sub

Private Sub mnuopen_Click()


Load frmformat
frmformat.Show
End Sub

Private Sub mnuregular_Click()


frmformat.txtdisplay.FontBold = False
frmformat.txtdisplay.FontItalic = False
End Sub
Part A 3. Alarm Clock

Private Sub cmdsetalarm_Click()


mmcplayer1.Command = "Open"
End Sub

Private Sub cmdstopalarm_Click()


mmcplayer1.Command = "Close"
End Sub

Private Sub Form_Load()


mmcplayer1.DeviceType = "WaveAudio"
mmcplayer1.FileName = "d:\callvoice.wav"
End Sub

Private Sub Timer1_Timer()


Dim alarmtime As String
lblcurrenttime.Caption = Format(Now, "hh:mm:ssAM/PM")
alarmtime = Format(dtpalarmtime.Value, "hh:mm:ssAM/PM")
If alarmtime = lblcurrenttime.Caption Then
mmcplayer1.Command = "Play"
End If
End Sub
Part A 4. Sequential File

Dim filename As String, filenum As Integer


Private Sub cmdaddtofile_Click()
filenum = FreeFile
Open filename For Append As #filenum
Write #filenum, txtname.Text, txtaddress.Text, txtcity.Text, txtpin.Text, txtphone.Text
Close #filenum
MsgBox "Contact details saved in file."
txtname.Text = ""
txtaddress.Text = ""
txtcity.Text = ""
txtpin.Text = ""
txtphone.Text = ""
End Sub

Private Sub cmdviewfile_Click()


Dim cname As String, caddress As String, ccity As String, cpin As String, cphone As
String
Me.Cls
filenum = FreeFile
Open filename For Input As #filenum
Do Until EOF(filenum)
Input #filenum, cname, caddress, ccity, cpin, cphone
Print cname & vbTab & caddress & vbTab & ccity & vbTab & cpin & vbTab & cphone
Loop
Close #filenum
End Sub

Private Sub Form_Load()


filename = "mycontacts.dat"
End Sub
Part A 5. Employee Details

Private Sub cmbdesignation_KeyPress(KeyAscii As Integer)


KeyAscii = 0
End Sub

Private Sub cmdpayout_Click()


If cmbdesignation.Text = "" Then
MsgBox "Please select designation"
Exit Sub
End If
If txtempname.Text = "" Then
MsgBox "Please entter employee name"
Exit Sub
End If
If txtempid.Text = "" Then
MsgBox "Please employee ID"
Exit Sub
End If
Select Case cmbdesignation
Case "Software Engineer"
txtda = Val(txtbasicpay) * 20 / 100
txthra = Val(txtbasicpay) * 30 / 100
txtdeduction = Val(txtbasicpay) * 10 / 100
Case "Database Administrator"
txtda = Val(txtbasicpay) * 20 / 100
txthra = Val(txtbasicpay) * 35 / 100
txtdeduction = Val(txtbasicpay) * 11 / 100
Case "Testing Engineer"
txtda = Val(txtbasicpay) * 20 / 100
txthra = Val(txtbasicpay) * 25 / 100
txtdeduction = Val(txtbasicpay) * 9 / 100
End Select
txtgrosspay = Val(txtbasicpay) + Val(txtda) + Val(txthra)
txtnetsalary = Val(txtgrosspay) - Val(txtdeduction)
End Sub

Private Sub txtbasicpay_KeyPress(KeyAscii As Integer)


If Not Chr(KeyAscii) Like "[0-9]" And Not KeyAscii = 8 Then
MsgBox "Please enter only numerals"
KeyAscii = 0
End If
End Sub

Private Sub txtempid_KeyPress(KeyAscii As Integer)


If Not Chr(KeyAscii) Like "[0-9,A-Z]" And Not KeyAscii = 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtempname_KeyPress(KeyAscii As Integer)
If Not Chr(KeyAscii) Like "[A-Z,., ,a-z]" And Not KeyAscii = 8 Then
KeyAscii = 0
End If
End Sub
Part A 6. Vending Machine

Dim rates(4) As Integer


Dim billamount As Integer, billsummary As String

Private Sub cmdbill_Click()


lblbill.Caption = ""
lblbill.Caption = billsummary
lblbill.Caption = lblbill.Caption & vbNewLine & "Total amount: Rs. " & billamount & "/-"
End Sub
Private Sub cmddispense_Click()
Dim i As Integer
For i = 0 To 3
If optsnacks(i).Value = True Then
lblbill.Caption = optsnacks(i).Caption & "Rs. " & rates(i) & "/- X " & Val(txtqty.Text)
& " = " & rates(i) * Val(txtqty.Text)
billsummary = billsummary & vbNewLine & lblbill.Caption
billamount = billamount + rates(i) * Val(txtqty.Text)
End If
Next i
End Sub

Private Sub Form_Load()


rates(0) = 100
rates(1) = 150
rates(2) = 50
rates(3) = 200
End Sub
Part A 7. User Authentication

Option Explicit
Dim dbcon As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub cmdCancel_Click()


End
End Sub

Private Sub cmdOK_Click()


If txtUserName.Text = "" Then
MsgBox "Please enter username"
Exit Sub
End If
If txtPassword.Text = "" Then
MsgBox "please enter password"
Exit Sub
End If
If rs.State = 1 Then
rs.Close
Set rs = Nothing
End If
rs.Open "select * from login14yasb where username = '" & Trim(txtUserName.Text) & "'
and password = '" & Trim(txtPassword.Text) & "'", dbcon, 1, 2
If rs.RecordCount > 0 Then
MsgBox "user credentials are authenticated successfully"
Else
MsgBox "Invalid credentials"
End If
End Sub

Private Sub Form_Load()


dbcon.Open "libdsn", "scott", "tiger"
End Sub

Private Sub Form_Unload(Cancel As Integer)


If dbcon.State = 1 Then
dbcon.Close
Set dbcon = Nothing
End If
End Sub
Part A 8. Book Search

Dim db As New ADODB.Connection


Dim rs As New ADODB.Recordset

Private Sub cmdsearch_Click()


If rs.State = 1 Then
rs.Close
Set rs = Nothing
End If
Select Case cmbsearchtype.Text
Case "ISBN"
rs.Open "select * from presilib where isbn = '" & Trim(txtsearchstring.Text) & "'",
db, adOpenDynamic, adLockOptimistic

Case "TITLE"
rs.Open "select * from presilib where title = '" & Trim(txtsearchstring.Text) & "'",
db, 1, 2

Case "AUTHOR"
rs.Open "select * from presilib where author = '" & Trim(txtsearchstring.Text) & "'",
db, 1, 2

Case Else
rs.Open "select * from presilib", db, 1, 2

End Select

Set DataGrid1.DataSource = rs
End Sub

Private Sub Form_Load()


db.Open "libdsn", "scott", "tiger"
End Sub

Private Sub Form_Unload(Cancel As Integer)


If db.State = 1 Then
db.Close
Set db = Nothing
End If
End Sub
Part A 9. VC++ Dialog Box

#include <afxwin.h>
class Myframe:public CFrameWnd
{
public:
Myframe()
{
Create(NULL,"SIMPLE DIALOG Box");
}
void OnLButtonDown(UINT flag, CPoint point)
{
char str[100];
sprintf(str,"%d, %d",point.x,point.y);
MessageBox(str, "Mouse position");
}
DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(Myframe,CFrameWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

class Myapp:public CWinApp


{
public:
BOOL InitInstance()
{
Myframe *bwnd;
bwnd = new Myframe;
bwnd->ShowWindow(1);
m_pMainWnd = bwnd;
return 1;
}
};
Myapp theApp;
Part A 10. VC++ Menu Creation

#include <afxwin.h>
#include "resource.h"
class myframe : public CFrameWnd
{
public :
myframe()
{
Create(NULL,"menu
program",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}
};
class Myapp:public CWinApp
{
public:
BOOL InitInstance()
{
myframe *bw;
bw=new myframe();
bw->ShowWindow(1);
m_pMainWnd=bw;
return 1;
}
};
Myapp theApp;
Part B
Part B 11. Calculator (Control Arrays)
11. Write a VB program to design a simple calculator to perform addition,
subtraction, multiplication and division (control Arrays)

Dim operand1 As Single, operand2 As Single, result As Single


Dim oper As String, flag As Boolean

Private Sub cmdkeypad_Click(Index As Integer)


If Index <= 10 Then
If flag = True Then
txtdisplay.Text = cmdkeypad(Index).Caption
flag = False
Else
txtdisplay.Text = txtdisplay.Text & cmdkeypad(Index).Caption
End If
ElseIf Index > 10 And Index <= 16 Then
operand1 = Val(txtdisplay.Text)
oper = cmdkeypad(Index).Caption
txtdisplay.Text = oper
flag = True
ElseIf Index = 17 Then
txtdisplay.Text = ""
Else
operand2 = Val(txtdisplay.Text)
Select Case oper
Case "+"
result = add((operand1), (operand2))
Case "-"
result = subtract((operand1), (operand2))
Case "*"
result = mult((operand1), (operand2))
Case "/"
result = divide((operand1), (operand2))
Case "\"
result = intdivide((operand1), (operand2))
Case "mod"
result = remainder((operand1), (operand2))
End Select
txtdisplay.Text = result
End If
End Sub

Private Function add(ByVal n1 As Single, ByVal n2 As Single) As Single


add = n1 + n2
End Function
Private Function subtract(ByVal n1 As Single, ByVal n2 As Single) As Single
subtract = n1 - n2
End Function
Private Function mult(ByVal n1 As Single, ByVal n2 As Single) As Single
mult = n1 * n2
End Function
Private Function divide(ByVal n1 As Single, ByVal n2 As Single) As Single
divide = n1 / n2
End Function
Private Function intdivide(ByVal n1 As Single, ByVal n2 As Single) As Single
intdivide = n1 \ n2
End Function
Private Function remainder(ByVal n1 As Single, ByVal n2 As Single) As Single
remainder = n1 Mod n2
End Function
Part B 12. VB Application to change the color of the text in a text box use
scroll bar for the selection of colors

Private Sub HScroll1_Change()


txtdisplay.ForeColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)
lblred.Caption = HScroll1.Value
End Sub

Private Sub HScroll2_Change()


txtdisplay.ForeColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)
lblgreen.Caption = HScroll2.Value
End Sub

Private Sub HScroll3_Change()


txtdisplay.ForeColor = RGB(HScroll1.Value, HScroll2.Value, HScroll3.Value)
lblblue.Caption = HScroll3.Value
End Sub
Part B 13. AVI Player
Part B 13. VB Application to develop a simple AVI Player
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal
lpszSoundName As String, ByVal uflags As Long) As Long

Private Sub cmdPlaySound_Click(Index As Integer)


Dim result As Long, sname As String
Dim soundfile As String
Select Case Index
Case 0
sname = "chimes.wav"
Case 1
sname = "chord.wav"
Case 2
sname = "ding.wav"
Case 3
sname = "tada.wav"
Case 4
sname = "ringin.wav"
Case 5
sname = "notify.wav"
Case 6
sname = "Windows XP Startup.wav"
End Select
soundfile = "c:\windows\media\" & sname
result = sndPlaySound(soundfile, SND_ASYNC)
End Sub

Private Sub dirControl_Change()


fileControl.Path = dirControl.Path
End Sub

Private Sub drvControl_Change()


dirControl.Path = drvControl.Drive & "\"
End Sub

Private Sub fileControl_Click()


If LCase(Right(fileControl.filename, 3)) = "avi" Then
MMControl1.Command = "close"
MMControl1.filename = fileControl.Path & "\" & fileControl.filename
Picture1.Cls
Picture1.Width = 5500
Picture1.Height = 6400
MMControl1.Command = "open"
End If
End Sub
Private Sub Form_Load()
MMControl1.Command = "open"
MMControl1.hWndDisplay = Picture1.hWnd
End Sub
Part B 14.Binary File
VB Application to write into a Binary File and To Read from a binary File

Private Sub Command1_Click()


Dim varout As Double
varout = Val(Text1.Text)
On Error GoTo FileError
Open "D:\BINARY.TXT" For Binary As #1
Put #1, , varout
Close #1
Exit Sub

FileError:
MsgBox "File Error!"
End Sub

Private Sub Command2_Click()


Dim varIn As Double
On Error GoTo FileError
Open "D:\BINARY.TXT" For Binary As #1
Get #1, , varIn
Text2.Text = Str(varIn)
Close #1
Exit Sub

FileError:
MsgBox "File Error!"
End Sub
Part B 15.Encryption & Decryption
21. VB program to Encrypt and Decrypt a string.(Use Rnd() to generate the Encryption and Decryption keys.)

Public rno As Integer


Private Sub cmd_clear_Click()
txt_encdec.Text = ""
txt_encdec.Setfocus
End Sub

Private Sub cmd_encdec_Click()


Dim l As Integer
Dim decr As String

i=1
l = Len(txt_encdec)
Do While (i <= l)
encstr = Mid(txt_encdec, i, 1)
dec = dec + Chr(Asc(encstr) - rno)
i=i+1
Loop
txt_encdec = dec
End Sub

Private Sub cmd_exit_Click()


Unload Me
End Sub

Private Sub Form_Load()


Randomize
rno = Rnd() * 100 + 1
End Sub

Private Sub txt_encdec_KeyPress(KeyAscii As Integer)


KeyAscii = KeyAscii + rno
End Sub
Part B 16. Image Animation
16. Write a VB program to Animate an Image
Dim direction As Integer, leftlimit As Integer, rightlimit As Integer, bottomlimit As Integer,
toplimit As Integer
Dim direc As String, speed As String

Private Sub cmdFast_Click()


Timer1.Interval = 5
speed = "Fast"
End Sub
Private Sub cmdMedium_Click()
Timer1.Interval = 50
speed = "Medium"
End Sub
Private Sub cmdSlow_Click()
Timer1.Interval = 300
speed = "Slow"
End Sub

Private Sub cmdDown_Click()


direction = 2
End Sub

Private Sub cmdLeft_Click()


direction = 3
End Sub

Private Sub cmdRight_Click()


direction = 1
End Sub

Private Sub cmdUp_Click()


direction = 0
End Sub

Private Sub Form_Load()


leftlimit = Frame2.Left
toplimit = Frame2.Top
rightlimit = Frame2.Width - Image1.Width
bottomlimit = Frame2.Height - Image1.Height
speed = "Slow"
End Sub

Private Sub Timer1_Timer()


If direction = 0 Then
Image1.Top = Image1.Top - 10
cmdDown.Enabled = True
cmdUp.Enabled = False
cmdLeft.Enabled = True
cmdRight.Enabled = True
direc = "Upward - "
End If
If direction = 1 Then
Image1.Left = Image1.Left + 10
cmdLeft.Enabled = True
cmdDown.Enabled = True
cmdRight.Enabled = False
cmdUp.Enabled = True
direc = "Right - "
End If
If direction = 2 Then
Image1.Top = Image1.Top + 10
cmdUp.Enabled = True
cmdDown.Enabled = False
cmdLeft.Enabled = True
cmdRight.Enabled = True
direc = "Downward - "
End If
If direction = 3 Then
Image1.Left = Image1.Left - 10
cmdRight.Enabled = True
cmdLeft.Enabled = False
cmdUp.Enabled = True
cmdDown.Enabled = True
direc = "Left - "
End If
If Image1.Top = toplimit Then
direction = 2
cmdUp.Enabled = True
cmdDown.Enabled = False
cmdLeft.Enabled = True
cmdRight.Enabled = True
End If
If Image1.Top > bottomlimit Then
direction = 0
cmdDown.Enabled = True
cmdUp.Enabled = False
cmdLeft.Enabled = True
cmdRight.Enabled = True
End If
If Image1.Left = leftlimit Then
direction = 1
cmdLeft.Enabled = True
cmdDown.Enabled = True
cmdRight.Enabled = False
cmdUp.Enabled = True
End If
If Image1.Left > rightlimit Then
direction = 3
cmdRight.Enabled = True
cmdLeft.Enabled = False
cmdUp.Enabled = True
cmdDown.Enabled = True
End If
Frame2.Caption = direc & speed
End Sub
Part B 17. VB program Fractional Number Validation
Dim period As Integer, sign As Integer, digicount As Integer

Private Sub txtFractNum_KeyPress(KeyAscii As Integer)


If digicount > 0 And KeyAscii = 45 Then
MsgBox "Sign should appear at first"
KeyAscii = 0
digicount = 0
End If

If KeyAscii = 45 Then
sign = sign + 1
End If
If KeyAscii = 46 Then
period = period + 1
End If
If period > 1 Then
MsgBox "A number should have only one integral part and one fractional part"
period = period - 1
KeyAscii = 0
End If
If Not Chr(KeyAscii) Like "[0-9]" And Not Chr(KeyAscii) = "." And Not Chr(KeyAscii) = "-"
And Not KeyAscii = 8 And Not KeyAscii = 27 Then
MsgBox "Invalid Keypressed"
KeyAscii = 0
End If
digicount = digicount + 1
If KeyAscii = 8 Then
sign = sign - 1
End If
End Sub

Private Sub txtFractNum_LostFocus()


If period = 0 Then
MsgBox "There is no fractional part. Please Re-enter the number."
txtFractNum.Text = ""
Else
MsgBox "Number is valid."
period = 0
sign = 0
digicount = 0
txtFractNum.Text = ""
End If
End Sub
Part B 18. Scrollbar Calculator
Dim hsc As Integer

Private Sub cmdDiff_Click()


lblresult.Caption = "Difference"
txtresult.Text = Val(Trim(txtnum1.Text)) - Val(Trim(txtnum2.Text))
HScroll1.Visible = False
End Sub

Private Sub cmdprod_Click()


lblresult.Caption = "Product"
txtresult.Text = Val(Trim(txtnum1.Text)) * Val(Trim(txtnum2.Text))
HScroll1.Visible = False
End Sub

Private Sub cmdquot_Click()


lblresult.Caption = "Quotient"
txtresult.Text = Val(Trim(txtnum1.Text)) / Val(Trim(txtnum2.Text))
HScroll1.Visible = False
End Sub

Private Sub cmdSum_Click()


lblresult.Caption = "Sum"
txtresult.Text = Val(Trim(txtnum1.Text)) + Val(Trim(txtnum2.Text))
HScroll1.Visible = False
End Sub

Private Sub HScroll1_Change()


If hsc = 1 Then
txtnum1.Text = HScroll1.Value
Else
txtnum2.Text = HScroll1.Value
End If
txtresult.Text = ""
lblresult.Caption = "Result"
End Sub

Private Sub txtnum1_GotFocus()


HScroll1.Visible = True
HScroll1.Left = txtnum1.Left
HScroll1.Top = txtnum1.Top - 300
hsc = 1
HScroll1.Value = 0
End Sub

Private Sub txtnum2_GotFocus()


HScroll1.Visible = True
HScroll1.Left = txtnum2.Left
HScroll1.Top = txtnum2.Top - 300
hsc = 2
HScroll1.Value = 0
End Sub
Part B 19. Screen Saver
Dim f As Boolean, i As Long

Private Sub Form_Click()


End
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)


End
End Sub

Private Sub Form_Load()


Me.Hide
Me.MousePointer = 1
End Sub

Private Sub Timer1_Timer()


Me.Show
Timer1.Enabled = False
End Sub

Private Sub Timer2_Timer()


Dim r As Integer, g As Integer, b As Integer
r = (Rnd * i) Mod 256
g = (Rnd * i) Mod 256
b = (Rnd * i) Mod 256
Label1.ForeColor = RGB(r, g, b)
i = i + 100
If i > 100000 Then
i=0
End If
Label1.Left = Me.Width / 2 - Label1.Width / 2
Label1.Top = Me.Height / 2 - Label1.Height / 2
End Sub
Part B 20. Splash Screen
Private Sub Timer1_Timer()
Static n As Integer
If ProgressBar1.Value < 100 Then
ProgressBar1.Value = ProgressBar1.Value + 1
Label4.Caption = ProgressBar1.Value & " %"
Else
Timer1.Enabled = False
Unload Me
Load frmMDI
frmMDI.Show
End If
End Sub

You might also like