Uploaded by Ashlee Mbengo

vb codes 2015

advertisement
Code to compute the roots of a quadratic equation, displaying result on a picture box
Private Sub cmdCompute_Click()
Dim a, b, c, m, n As Single
a = Val(a.Text)
b = Val(b.Text)
c = Val(c.Text)
m = (-b - Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
n = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
picResult.Cls
picResult.Print m; " or "; n
End Sub
Code for calculating the volume of a cylinder
Private Sub OK_Click()
r = Val(radius.Text)
h = Val(hght.Text)
pi = 22 / 7
v = pi * (r ^ 2) * h
volume.Text = Str$(v)
End Sub
Code for grading marks of a students
Dim mark As Single
Private Sub Command1_Click()
mark = txtMark.Text
Select Case mark
Case 0 To 49
Display.Caption = "F"
Case 50 To 64
Display.Caption = "C"
Case 65 To 79
Display.Caption = "B"
Case 80 To 100
Display.Caption = "A"
Case Else
Display.Caption = "INVALID"
End Select
End Sub
maregat@mbhs2015
Code to compute the factorial of a number
Private Sub Fact_Click()
txtFact = 1
For i = 1 To txtMark.Text
txtFact.Text = txtFact * i
Next
End Sub
Code for calculating the future value based on Interest
Private Sub Calculate_Click()
Dim p, r, t, I, FV As Single
p = Val(txtP.Text)
r = Val(txtR.Text)
t = Val(txtT.Text)
I = ((p * r * t) / 100)
FV = p + I
picFV.Print "The future value is:
"; FV
End Sub
Code for comparing two given numbers
Dim answer As String
Private Sub cmdComp_Click()
a = txtnum1.Text
b = txtnum2.Text
If a > b Then
answer = MsgBox(a & "--" & "is greater than" & "- . -" & b, vbOKOnly, "Comparisons")
Else
If a < b Then
answer = MsgBox(a & "--" & "is less than" & "- . -" & b, vbOKOnly, "Comparisons")
Else
answer = MsgBox(a & "--" & "is equal to" & " - . -" & b, vbOKOnly, "Comparisons")
End If
End If
End Sub
maregat@mbhs2015
Code for comparing two given numbers and display result on a picture box
Private Sub Comparison_Click()
Dim a, b As Single
a = txtNum1.Text
b = txtNum2.Text
If a < b Then
picComp.Cls
picComp.Print a; " is less than "; b
ElseIf a > b Then
picComp.Cls
picComp.Print a; " is greater than "; b
Else
picComp.Cls
picComp.Print a; " is equal to "; b
End If
End Sub
Code for a standard calculator
Dim result As Double
Dim op As String
Private Sub cmdAdd_Click()
op = "+"
result = Val(txtDisplay.Text)
txtDisplay.Text = ""
End Sub
Private Sub cmdAns_Click()
Select Case (op)
'after pressing the ADD command, this code is executed
Case "+"
txtDisplay.Text = result + Val(txtDisplay.Text)
Case "-"
result = result - Val(txtDisplay.Text)
txtDisplay.Text = result
maregat@mbhs2015
Case "/"
result = result / Val(txtDisplay.Text)
txtDisplay.Text = result
Case "x"
result = result * Val(txtDisplay.Text)
txtDisplay.Text = result
End Select
End Sub
Private Sub cmdDiv_Click()
op = "/"
result = Val(txtDisplay.Text)
txtDisplay.Text = ""
End Sub
Private Sub cmdMult_Click()
op = "x"
result = Val(txtDisplay.Text)
txtDisplay.Text = ""
End Sub
Private Sub cmdSub_Click()
op = "-"
result = Val(txtDisplay.Text)
txtDisplay.Text = ""
End Sub
Private Sub Command1_Click()
txtDisplay.Text = txtDisplay.Text + "1"
End Sub
maregat@mbhs2015
Private Sub Command10_Click()
txtDisplay.Text = txtDisplay.Text + "6"
End Sub
Private Sub Command11_Click()
txtDisplay.Text = txtDisplay.Text + "9"
End Sub
Private Sub Command2_Click()
txtDisplay.Text = txtDisplay.Text + "4"
End Sub
Private Sub Command3_Click()
txtDisplay.Text = txtDisplay.Text + "7"
End Sub
Private Sub Command4_Click()
txtDisplay.Text = ""
End Sub
Private Sub Command5_Click()
txtDisplay.Text = txtDisplay.Text + "2"
End Sub
Private Sub Command6_Click()
txtDisplay.Text = txtDisplay.Text + "5"
End Sub
Private Sub Command7_Click()
txtDisplay.Text = txtDisplay.Text + "8"
End Sub
maregat@mbhs2015
Private Sub Command8_Click()
txtDisplay.Text = txtDisplay.Text + "0"
End Sub
Private Sub Command9_Click()
txtDisplay.Text = txtDisplay.Text + "3"
End Sub
Code for traffic lights
Dim Counter As Variant
Private Sub Form_Load()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Counter = 0
Counter = Counter + 1
If Counter = Counter + 10 Then
Shape1.Visible = True
Shape2.Visible = False
Shape3.Visible = False
ElseIf Counter = Counter + 30 Then
Shape2.Visible = True
Shape1.Visible = False
Shape3.Visible = False
ElseIf Counter = Counter + 40 Then
Shape3.Visible = True
Shape1.Visible = False
Shape2.Visible = False
End If
End Sub
maregat@mbhs2015
Download