1) Write a VB Program to design a simple calculator to ...
Transcript of 1) Write a VB Program to design a simple calculator to ...
1) Write a VB Program to design a simple calculator to perform addition,
subtraction, multiplication and division (Use functions for the calculations).
Dim x As Double, y As Double
Dim z As Boolean
Dim opr As String
Private Sub cmdadd_Click ()
x=Val(lblresult.Caption)
opr = "+"
lblresult.Caption = " "
End Sub
Private Sub cmdclear_Click()
lblresult.Caption = ""
End Sub
Private Sub cmddiv_Click ()
x=Val(lblresult.Caption)
opr = "/"
lblresult.Caption = " "
End Sub
Private Sub cmddot_Click ()
If z = True Then
lblresult.Caption=""
z=False
End If
If InStr(lblresult.Caption, ".") Then
Exit Sub
Else
lblresult.Caption = lblresult.Caption + "."
End If
End Sub
Private Sub cmdequals_Click()
Dim result As Double
y = Val(lblresult.Caption)
If opr ="+" Then
result=add(x, y)
End If
If opr = "-" Then
result = subtract(x, y)
EndIf
If opr="*" Then
result=mul(x, y)
EndIf
If opr="/" Then
result=div(x, y)
End If
lblresult.Caption=result
End Sub
Private Sub cmdmul_Click()
x=Val(lblresult.Caption)
opr = "*"
lblresult.Caption = " "
End Sub
Private Sub cmdsbtract_Click()
x=Val(lblresult.Caption)
opr = "-"
lblresult.Caption = " "
End Sub
Private Sub Label1_Click()
End
End Sub
Private Sub Number_Click(Index As Integer)
If z Then
lblresult.Caption = ""
z = False
End If
lblresult.Caption = lblresult.Caption +Number(Index).Caption
End Sub
Function add(ByVal x As Double, ByVal y As Double) As Double
add = x +y
End Function
Function subtract(ByVal x As Double, ByVal y As Double) As Double
subtract = x - y
End Function
Function mul(ByVal x As Double, ByVal y As Double) As Double
mul = x * y
End Function
Function div(ByVal x As Double, ByVal y As Double) As Double
div = x / y
End Function
2) Design a User Interface (UI) to accept the student details such as name,
department and total marks. Validate the input data and calculate the
percentage and division.
Private Sub Command1_Click()
Dim avg As Single
If Len(Text3.Text) = 0 Or Len(Text4.Text) = 0 Or Len(Text5.Text) = 0 Or Len(Text6.Text) = 0 Or
Len(Text7.Text) = 0 Then
MsgBox (" Field Should not be Empty ")
Exit Sub
End If
Label10.Caption=Val(Text3.Text)+Val(Text4.Text)+Val(Text5.Text)+Val(Text6.Text) +
Val(Text7.Text)
Label12.Caption = Val(Label10.Caption) / 5
avg = Val(Label12.Caption)
If (avg >= 75) Then
Label14.Caption = "Distinction"
ElseIf (avg >= 60) Then
Label14.Caption = "First Class"
ElseIf (avg >= 50) Then
Label14.Caption = "Second Class"
ElseIf (avg >= 35) Then
Label14.Caption = "Third Class"
Else
Label14.Caption = "fail"
End If
If Val(Text3.Text) < 35 Or Val(Text4.Text) < 35 Or Val(Text5.Text) < 35 Or Val(Text6.Text) < 35
Or Val(Text7.Text) < 35 Then Label14 = "fail"
End If
End Sub
Private Sub Command2_Click()
Text1.Text = " "
Text2.Text = " "
Text3.Text = " "
Text4.Text = " "
Text5.Text = " "
Text6.Text = " "
Text7.Text = " "
Label10.Caption = " "
Label12.Caption = " "
Label14.Caption = " "
End Sub
Private SubCommand3_Click()
End
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((KeyAscii>= 65 And KeyAscii<= 91) Or (KeyAscii>= 97 And KeyAscii<= 123)
Or KeyAscii = 32 Or KeyAscii = 8) Then
Else
KeyAscii = 0
MsgBox("Enter only Alphabets")
End If
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If (KeyAscii>= 48 And KeyAscii<= 57)
Then
Else
KeyAscii = 0
MsgBox ("Enter onlyDigits")
End If
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
If (KeyAscii>= 48 And KeyAscii<= 57) Then
Else
KeyAscii = 0
MsgBox ("Enter only Digits")
End If
End Sub
Private Sub Text5_KeyPress(KeyAscii As Integer)
If (KeyAscii>= 48 And KeyAscii<= 57) Then
Else
KeyAscii = 0
MsgBox ("Enter onlyDigits")
End If
End Sub
Private Sub Text6_KeyPress(KeyAscii As Integer)
If (KeyAscii>= 48 And KeyAscii<= 57) Then
Else
KeyAscii = 0
MsgBox ("Enter only Digits")
End If
End Sub
Private Sub Text7_KeyPress(KeyAscii As Integer) If (KeyAscii>= 48 And KeyAscii<= 57) Then Else KeyAscii = 0 MsgBox ("Enter only Digits") End If End Sub
3) Design a VB application which has MDI and Child forms. Create a menu
having the items such as file (New, Open), Format (Font, Regular,
Bold,Italic) and Exit in the MDI form. Also create a text box and use a
Common Dialog Box control for changing the font, fore color and back color
of the text box.
Private Sub mnubackcolor_Click()
CommonDialog1.ShowColor
Text1.BackColor = CommonDialog1.Color
End Sub
Private Sub mnubold_Click()
CommonDialog1.ShowFont
Text1.Font = CommonDialog1.FontBold
End sub
Private Sub mnuchild_Click()
child1.Show
End Sub
Private Sub mnuchild2_Click()
child2.Show
End Sub
Private Sub mnuexit_Click()
End
End Sub
Private Sub mnufont_Click()
CommonDialog1.ShowFont
Text1.Font = CommonDialog1.FontName
EndSub
Private Sub mnuforecolor_Click()
CommonDialog1.ShowColor
Text1.ForeColor = CommonDialog1.Color
EndSub
Private Sub mnuitalic_Click()
CommonDialog1.ShowFont
Text1.Font = CommonDialog1.FontItalic
End Sub
Private Sub mnursize_Click()
CommonDialog1.ShowFont
Text1.Font = CommonDialog1.FontSize
End Sub
4) VB program to Encrypt and Decrypt a string. (Use Rnd() to generate the
Encryption and Decryption keys).
Dim dpt As String
Dim key As Integer
Dim rnum As Integer
Dim ept As String
Private Sub Command1_Click()
Text1.Text = ept
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim str2 As Stringdpt = ""
For i = 1 To Len(ept)
str2 = Mid(ept, i, 1)
str2 = Chr(Asc(str2) - rnum)
dpt = dpt + str2
Next i
Text1.Text = dpt
End Sub
Private Sub Command3_Click()
End
EndSub
Private Sub Form_Load()
ept =""
dpt = ""
Text1.Text = ""
rnum = (10 * Rnd + 1)
End Sub
Private Sub Text1_keypress(KeyAscii As Integer)
If (KeyAscii>= 65 And keyAcii<= 90) Or (KeyAscii>= 97 And KeyAscii<=
22) Or KeyAscii=32 Then
key = KeyAscii + rnum
ept = ept + Chr(key)
Else
KeyAscii = 0
End If
End Sub
5) Design a small Alarm Clock Application.
Private Sub Command1_Click()
Label2.Caption = Text1.Text
End Sub
Private Sub Command2_Click()
Timer1.Enabled = True
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Command4_Click()
End
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.InterVal = 500
Text1.Text = "00:00:00"
End Sub
Private Sub Timer1_Timer()
Dim i As Integer
Label3.Caption = Format(Time, "hh:mm:ss")
If Trim(Label3.Caption) >= Trim(Label2.Caption) Then
Beep
End If
End Sub
6) Write a VB Program to Validate the username and password form the
database and display the appropriate message.(Use Data Control)
Private Sub Command1_Click()
Dim x As Integer x = 0
Adodc1.Recordset.MoveFirst
Do While Not Adodc1.Recordset.EOF
If Text1.Text=Adodc1.Recordset.Fields(0) AndText2.Text=Adodc1.Recordset.Fields(1) Then
MsgBox ("Valid user name and password")
End If
Adodc1.Recordset.MoveNext
Loop
If x = 0 Then
MsgBox ("inValid username or password")
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
End Sub
Private Sub Command2_Click()
End
End Sub
7) Design a VB application to record the employee details such as EmpId,
EmpName, Designation and Basic Pay. Calculate the DA, HRA, Deduction
and Gross Salary. (Make the necessary assumptions )Use Select .. case for
decision making.
Private Sub Command1_Click()
Adodc1.Recordset.AddNew
End Sub
Private Sub Command2_Click()
Select Case cmddes
Case "manager"
txtda.Text = Val(txtbasic.Text) *10/100
txthra.Text=Val(txtbasic.Text)*15/100
txtded.Text = Val(txtbasic.Text) *12 / 100
txtgross.Text = Val(txtbasic.Text) + Val(txtda.Text) + Val(txthra.Text)
txtnet.Text = Val(txtgross.Text) - Val(txtded.Text)
Case "engineer"
txtda.Text = Val(txtbasic.Text) *10 /100
txthra.Text=Val(txtbasic.Text)*15 /100
txtded.Text = Val(txtbasic.Text) *12 / 100
txtgross.Text = Val(txtbasic.Text) + Val(txtda.Text) + Val(txthra.Text)
txtnet.Text = Val(txtgross.Text) – Val(txtded.Text)
Case "clerk"
txtda.Text = Val(txtbasic.Text) *10 /100
txthra.Text=Val(txtbasic.Text)*15 /100
txtded.Text = Val(txtbasic.Text) *12 / 100
txtgross.Text = Val(txtbasic.Text) + Val(txtda.Text) + Val(txthra.Text)
txtnet.Text = Val(txtgross.Text) – Val(txtded.Text)
EndSelect
EndSub
Private Sub Command3_Click()
Adodc1.Recordset.Update
End Sub
Private Sub Command4_Click()
End End Sub
Private Sub Form_Load() cmddes.AddItem "manager" cmddes.AddItem "engineer"
cmddes.AddItem"Clerk"
End Sub
8) VB program to calculate the simple interest and compound interest. Use
DLLs for the calculation.
Public s1 As New SRIKANTH.class1
Private Sub Command1_Click()
Dim x As Integer
p = Val(Text1.Text)
t = Val(Text2.Text)
r = Val(Text3.Text)
x = s1.SI(p, t, r)
Label5.Caption = "Simple Interest"
Label6.Caption = x
End Sub
Private Sub Command2_Click()
Dim x As Integer
p = Val(Text1.Text)
t = Val(Text2.Text)
r = Val(Text3.Text)
x = s1.CI(p, t, r)
Label5.Caption = "Compound Interest"
Label6.Caption = x
End Sub
Private Sub Command3_Click()
End
End Sub
Class Module Code
Public Function CI (ByVal p As Double, ByVal t As Double, ByVal r As Double) As
Double
CI = (p * (1 + r / 100) ^ t) - p
End Function
Public Function SI (ByVal p As Double, ByVal t As Double, ByVal r As Double) As
Double
SI = (p * t * r) / 100
End Function
9) VC++ program to create a Dialog box and display the position of mouse
pointer within the dialog box.
10) VC++ program to create and load a simple menu in a Window.
Part-B
1. Program to find largest of three numbers. CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Text3 name Text3
Text (blank)
Command1 name Cmdlarg
Caption Largest
number
Command2 name cmdend
Caption end
Private Sub cmdlarg_Click()
Dim a, b, c As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
If ((a > b) And (a > c)) Then
MsgBox ("a Is largest")
ElseIf (b > c) Then
MsgBox ("b Is largest")
Else
MsgBox ("c Is largest")
End If
End Sub
Private Sub cmdend_Click()
End
End Sub
2.Program to find area and circumference of a circle.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name Cmdarea
Caption Area and circumference of a
circle
Command2 name cmdend
caption end
Private Sub cmdarea_Click()
Const pi As Single = 3.142
Dim r, area, cir As Single
r = Val(Text1.Text)
area = pi * r * r
cir = 2 * pi * r
Text2.Text = area
Text3.Text = cir
End Sub
Private Sub cmdend_Click()
End
End Sub
3. Program to check whether a number is odd or even.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdevenodd
Caption Even / odd
Command2 name cmdend
caption End
Private Sub cmdevenodd_Click()
Dim n As Integer
n = Val(Text1.Text)
If (n Mod 2) = 0 Then
Text2.Text = "Even number"
Else
Text2.Text = "Odd number"
End If
End Sub
Private Sub cmdend_Click()
End
End Sub
4. Program to find sum of natural numbers up to a given number.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdnatsum
Caption Sum of natural numbers
Command2 name cmdend
caption end
Private Sub cmdnatsum_Click()
Dim n, i, sum As Integer
n = Val(Text1.Text)
Print "natural numbers are"
For i = 1 To n
sum = sum + i
print i
Next i
Text2.Text = sum
End Sub
Private Sub cmdend_Click()
End
End Sub
5. Program to find Factorial of a given number.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdfact
Caption factorial
Command2 name cmdend
caption end
Private Sub cmdfact_Click()
Dim n, i, fact As Integer
n = Val(Text1.Text)
fact = 1
If (n < 0) Then
MsgBox ("No factorial for -ve numbers")
Else
For i = 1 To n
fact = fact * i
Next i
Text2.Text = fact
End If
End Sub
Private Sub cmdend_Click()
End
End Sub
6. Program to find sum of individual digits of a number.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmddigits
Caption Sum of individual digits
Command2 name cmdend
caption end
Private Sub cmddigits_Click()
Dim n, sum, digit As Integer
n = Val(Text1.Text)
sum = 0
Do While (n <> 0)
digit = n Mod 10
sum = sum + digit
n = n \ 10
Loop
Text2.Text = sum
End Sub
Private Sub cmdend_Click()
End
End Sub
7. Program to check whether a number is Palindrome or not.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdpal
Caption Palindrome or not
Command2 name cmdend
caption end
Private Sub cmdpal_Click()
Dim digit, temp, n, rev As Integer
n = Val(Text1.Text)
temp = n
rev = 0
Do While (n <> 0)
digit = n Mod 10
rev = rev * 10 + digit
n = n \ 10
Loop
Text2.Text = rev
If (temp = rev) Then
MsgBox ("Number is a palindrome")
Else
MsgBox ("Number is not a palindrome")
End If
End Sub
Private Sub cmdend_Click()
End
End Sub
8. Program to check whether a number is prime or not.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdprime
Caption Prime or not
Command2 name cmdend
caption end
Private Sub cmdprime_Click()
Dim i, n As Integer
n = Val(Text1.Text)
For i = 2 To n \ 2
If (n Mod i) = 0 Then
Text2.Text = "Not prime "
Exit Sub
End If
Next i
Text2.Text = "Prime"
End Sub
Private Sub cmdend_Click()
End
End Sub
9. Program to find sum of two matrices.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdfirst
Caption Input first matrix
Command2 name cmdsecond
caption Input second matrix
Command3 name cmdsum
caption Sum of 2 matrices
Dim a(2, 2), b(2, 2), c(2, 2), i, j, m, n As Integer
Private Sub cmdfirst_Click()
m = Val(Text1.Text)
n = Val(Text2.Text)
Print "First matrix"
For i = 1 To m
For j = 1 To n
a(i, j) = Val(InputBox("Enter the array elements one by one"))
Print a(i, j);
Next j
Next i
End Sub
Private Sub cmdsecond_Click()
Print "Second matrix"
For i = 1 To m
For j = 1 To n
b(i, j) = Val(InputBox("Enter the array elements one by one"))
Print b(i, j);
Next j
Next i
End Sub
Private Sub cmdsum_Click()
Print "sum of 2 matrices"
For i = 1 To m
For j = 1 To n
c(i, j) = a(i, j) + b(i, j)
Print c(i, j);
Next j
Next i
End Sub
10. Program to demonstrate the use of List box.
CONTROL PROPERTIES VALUE
Text1 name Text1
Text (blank)
Text2 name Text2
Text (blank)
Command1 name cmdadd
Caption add
Command2 name cmdremove
caption remove
Command3 name cmdclear
caption clear
Command4 name Cmdclose
Caption close
List1 name List1
list India
Usa
Private Sub cmdadd_Click()
List1.AddItem (Text1.Text)
Text1.Text = ""
Text2.Text = List1.ListCount
End Sub
Private Sub cmdclear_Click()
List1.Clear
Text2.Text = List1.ListCount
End Sub
Private Sub cmdclose_Click()
End
End Sub
Private Sub cmdremove_Click()
Dim ind As Integer
ind = List1.ListIndex
If ind >= 0 Then
List1.RemoveItem ind
Text2.Text = List1.ListCount
Else
Msgbox(“No item”)
End If
End Sub
Private Sub optred_Click()
Text1.ForeColor = vbRed
End Sub
Private Sub optgreen_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub optblue_Click()
Text1.ForeColor = vbBlue
End Sub