String Handling
Module Module1
'declare module level variables
Dim Full_name, Initials, Password As String
Dim Tens, Ones, Age, Sum As Integer
Dim ans As String = "y"
Sub Main()
Try
Do While (ans = "y")
'Display Welcome message & Today's Date
[Link]("Welcome User! Today is: " & [Link]() & vbCrLf)
'capture input from user
[Link]("Please Enter a full name")
Full_name = [Link]()
[Link]("Please enter Date of Birth in 'dd/Mon/yy' format")
'local variable declaration
Dim DOB As Date = [Link]([Link]()) 'or CDate(string value)
Age = [Link] - [Link]
Ones = Age Mod 10 'returns ones digit i.e remainder
Tens = Age \ 10 'returns tens digit when Integer Division is used and number divided by
10
Sum = Tens + Ones
Full_name = Full_name.Trim() 'remove spaces before & after
Dim SpaceIndex As Integer = Full_name.IndexOf(" ") 'determine position of space to
cut initials from lastname
'cut initials from first name and lastname
Initials = Full_name.Substring(0, 1) & Full_name.Substring(SpaceIndex + 1, 1)
Password = Initials + [Link]
[Link]("name: " & Full_name & " Age: " & Age & " Password: " & Password)
[Link]("Do you want to Continue with another password creation")
ans = [Link]()
Loop
Catch ex As FormatException
[Link]("Format Exception: " & [Link])
Catch ex As Exception
[Link]("General Exception: " & [Link])
End Try
End Sub
End Module
'try block ends here
'main sub-procedure end here
'module ends here
Decision Loops
Public NotInheritable Class AboutBox2
Private Sub AboutBox2_Load(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
' Set the title of the form.
Dim ApplicationTitle As String
If [Link] <> "" Then
ApplicationTitle = [Link]
Else
ApplicationTitle =
[Link]([Link])
End If
[Link] = [Link]("About {0}", ApplicationTitle)
' Initialize all of the text displayed on the About Box.
' TODO: Customize the application's assembly information in the "Application" pane of the
project
' properties dialog (under the "Project" menu).
[Link] = [Link]
[Link] = [Link]("Version {0}", [Link])
[Link] = [Link]
[Link] = [Link]
[Link] = [Link]
End Sub
Private Sub OKButton_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
End Class
Menus, functions & sub-procedures
Public Class Form1
'module level variable - if declared as Friend can be accessed in another form within same
project (namespace)
Friend FirstName As String
Dim myColor As ColorDialog
Dim myFont As FontDialog
Dim input As Integer
Private Sub CustomiseBack()
myColor = New ColorDialog
[Link]()
[Link] = [Link]
End Sub
'defining a sub procedure
Private Sub CustomiseTxT()
myColor = New ColorDialog
[Link]()
[Link] = [Link]
End Sub
Private Sub CloseRegFormToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Reg_Form.Close()
End Sub
Private Sub ExitToolStripMenuItem1_Click(ByVal sender As [Link], ByVal e As
[Link])
Reg_Form.Close()
[Link]()
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
input = [Link](InputBox("Enter a Number", "Recursion"))
'call to function called fact
Dim result As Integer = fact(input)
[Link]("Factorial : " & result)
[Link]("Input was : " & input)
End Sub
'factorial function - A recursive function
Private Function fact(ByVal num As Integer) As Integer
Dim prod As Integer
If num = 1 Then
Return 1
Exit Function
Else
prod = num * fact(num - 1) '<--Function calling itself is 'Recursion'
MsgBox("prod is : " & prod & " num is : " & num) '<--just observe them changing
End If
Return prod
End Function
'Demonstrate By Ref in Sub procedures & Functions
Private Sub IncrementVariable(ByVal Number2 As Integer)
Number2 = Number2 + 1
End Sub
Private Function IncrementFunction(ByRef Number2 As Integer)
Number2 = Number2 + 1
Return Number2
End Function
Private Sub ByVal_Btn_Click(ByVal sender As [Link], ByVal e As [Link])
Handles ByVal_Btn.Click
Dim Number1 As Integer
Number1 = 10
Call IncrementVar_ByVal(Number1)
'Value of Number1 stays the same
MsgBox(Number1)
End Sub
Private Sub IncrementVar_ByVal(ByVal Number2 As Integer)
Number2 = Number2 + 1
End Sub
Private Sub ByRef_Btn_Click(ByVal sender As [Link], ByVal e As [Link])
Handles ByRef_Btn.Click
Dim Number1 As Integer
Number1 = 10
Call IncrementVar_ByRef(Number1)
'Value of Number1 changes outside the procedure
'Dim x As Integer = IncrementFunction(Number1)
MsgBox(Number1)
End Sub
Private Sub IncrementVar_ByRef(ByRef Number2 As Integer)
Number2 = Number2 + 1
End Sub
'changing status message on button MouseHover Event
Private Sub ByRef_Btn_MouseHover(ByVal sender As [Link], ByVal e As
[Link]) Handles ByRef_Btn.MouseHover
[Link] = "This Button demonstrates ByRef usage in a sub-procedure and
function"
End Sub
'changing status message on button MouseHover Event
Private Sub ByVal_Btn_MouseHover(ByVal sender As [Link], ByVal e As
[Link]) Handles ByVal_Btn.MouseHover
[Link] = "This Button demonstrates ByVal usage in a sub-procedure"
End Sub
Private Sub ChangeBackColorToolStripMenuItem_Click(ByVal sender As [Link], ByVal e
As [Link]) Handles [Link]
With myColor
.Color = [Link]
.ShowDialog()
[Link] = .Color
End With
End Sub
Private Sub ChangeForeColorToolStripMenuItem_Click(ByVal sender As [Link], ByVal e
As [Link]) Handles [Link]
With myColor
.Color = [Link]
.ShowDialog()
[Link] = .Color
End With
End Sub
Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = [Link]
With FontDialog1
[Link] = [Link]
.ShowDialog()
'apply new selected font
[Link] = .Font
End With
End Sub
Private Sub Form1_Load(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = "This form demonstrates functions, procedures and menus "
End Sub
Private Sub OpenRegistrationForrmToolStripMenuItem_Click(ByVal sender As [Link],
ByVal e As [Link]) Handles [Link]
FirstName = [Link]
Reg_Form.Activate()
Reg_Form.Show()
[Link]()
End Sub
Private Sub CloseAllFormsToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Reg_Form.Close()
[Link]()
End Sub
Private Sub PickUrFavToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
'calling a sub procedure to change background color
Call CustomiseBack()
End Sub
Private Sub ChangeFormTextColorToolStripMenuItem_Click(ByVal sender As [Link],
ByVal e As [Link]) Handles [Link]
'call to sub procedure to customise text color
Call CustomiseTxT()
End Sub
Private Sub ExitToolStripMenuItem1_Click_1(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
MsgBox("Saying GoodBye!!")
Reg_Form.Close()
[Link]()
End Sub
Private Sub HelpToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
MsgBox("Sorry Work in Progress")
End Sub
Private Sub CloseRegistrationFormToolStripMenuItem_Click(ByVal sender As [Link],
ByVal e As [Link]) Handles [Link]
Reg_Form.Close()
End Sub
Private Sub Comp_ByVal_ByRef_Btn_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles Comp_ByVal_ByRef_Btn.Click
Dim Number1 As Integer
Dim Number2 As Double
Number1 = 5
Number2 = 10.5
'calling the sub procedure and passing parameters
CompareByValByRef(Number1, Number2)
'displaying the values for number1 and number2 after sub procedure call
MsgBox("Number1=" & Number1 & [Link] & "Number2=" & Number2)
End Sub
'sub-procedure definition; check parameter passing using - ByVal , ByRef
Private Sub CompareByValByRef(ByVal localNum1 As Integer, ByRef localNum2 As Double)
localNum1 += 5
localNum2 += 5
End Sub
Private Sub HelpToolStripMenuItem1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
myFont = New FontDialog
[Link]()
[Link] = [Link]
End Sub
Private Sub PickYoursToolStripMenuItem_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
CustomiseTxT()
End Sub
End Class
Error Handling
Public Class Error_Handling
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Try
Dim num1 As Integer = [Link]([Link])
Dim num2 As Integer = [Link]([Link])
' generates arithmatic exception on Integer Division (\) NOT in normal division (/)
Dim result As Double = num1 \ num2
[Link] = result
Catch ex As ArithmeticException
[Link]("Arithmatic exception: " & [Link])
Catch ex As InvalidCastException
[Link]("Invalid Cast Exception occured: (this is your own customised message
)" & [Link])
Catch ex As Exception
[Link]("General Exception: " & [Link])
Finally
[Link]("This block called 'Finally' is executed in the very end EVEN if NO
exception has occured." & _
[Link] & "SO WISHING YOU GOODBYE ---THE END------")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = [Link]
[Link] = ""
[Link]()
End Sub
End Class
Select Case
Public Class Ch4_select_case
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'Try
'Dim marks As Double = [Link]([Link])
Dim marks As Double = InputBox("Enter Marks %age")
Select Case marks
Case 75 To 100
[Link]("Distinction")
Case 60 To 74
[Link]("Credit")
Case 40 To 59
[Link]("Pass")
Case 0 To 40 ' or case Is < 40
[Link]("Fail")
Case Else
[Link]("Wrong %age entered")
End Select
'Catch TheException As FormatException
'[Link]("please input numbers only")
'Catch TheException As InvalidCastException
'[Link]("invalid entry")
'End Try
End Sub
Private Sub Ch4_select_case_Load(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
End Sub
End Class
Enhanced MessageBox
Public Class enhancedMessageBox
Dim MessageString As String
Dim NoOfOrders As Integer
Dim GrandTotalDecimal As Decimal
Dim AvgDecimal As Decimal
Dim flag As Boolean
Dim dr As DialogResult
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'Do While flag = True
dr = [Link]
Do While dr = [Link]
NoOfOrders = InputBox("Enter No of Orders")
GrandTotalDecimal = InputBox("Enter Total Sales")
AvgDecimal = GrandTotalDecimal / NoOfOrders
'create multiline formatted message string
MessageString = " Number of Orders : " & [Link]("N0") &
[Link] & " Average Sales: " & [Link]("C")
'display messagebox with selected buttons and icon
[Link](MessageString, "Sales Summery", [Link],
[Link])
dr = [Link]("Would you like to do another calculation?", "Enter your choice",
[Link], [Link])
[Link](" you clicked " & dr)
If dr = [Link] Then
Exit Do
End If
'[Link]("x " & dr)
Loop
End Sub
End Class
If_Then_While_Else_For
Public Class if_else_loops_for
Public fname As String
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim age As Integer = [Link]
If age < 18 Then
[Link]("Minor")
Else
[Link]("Adult")
End If
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = [Link]()
End Sub
Private Sub Button3_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = [Link]()
End Sub
Private Sub Button4_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'showing nested if statements
Dim temp = [Link]
If temp <= 32 Then
[Link] = "freezing"
ElseIf temp > 80 Then
[Link] = "hot"
Else
[Link] = "moderate"
End If
End Sub
Private Sub Button5_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim J As Integer
Dim Sum As Integer = 0
For J = 1 To 4 Step 1 ' reverse loop For J =5 To1 Step -1
Dim InValue As Integer = InputBox("Enter your marks")
Sum = Sum + InValue
[Link](Sum)
Next J
[Link]("Average = " & Sum / 4)
End Sub
Private Sub Button7_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
For J As Integer = 0 To 5
For K As Integer = 0 To 5
[Link] = [Link] & K
Next K
[Link] = [Link] & [Link]
Next J
End Sub
Private Sub FAct_Click(ByVal sender As [Link], ByVal e As [Link]) Handles
[Link]
Dim value As Integer = InputBox("Enter a number")
Dim prod As Integer = 1
Dim j = 1
Do Until (j = value + 1)
prod *= j
'(prod=prod*j)
j += 1
'(j=j+1)
Loop
[Link]("factorial :" & prod)
'Dim result As Integer = x
'For j As Integer = 1 To result
'If j = 1 Then
'Exit For
'Else
'result = result * (j)
'[Link]("result is " & result & "j is" & j)
'End If
'Next j
End Sub
Private Sub oddEven_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
For x = 1 To 10
If x = 5 Then
Continue For
End If
If x Mod 2 = 0 Then
[Link](x & " is even number")
Else
[Link](x & " is odd number")
End If
'[Link]("value of x is " & x)
'Exit For
Next x
End Sub
Private Sub if_then_else_Load(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'[Link] = True
End Sub
Private Sub Button8_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
If [Link] > [Link] Then
[Link]([Link])
ElseIf [Link] < [Link] Then
[Link]([Link])
Else
[Link]("Draw")
End If
End Sub
Private Sub Button9_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim x = 1
Do While x < 10
'If x Mod 2 = 0 Then
If x = 5 Then
Continue Do
'End If
[Link](x & " is even number")
Else
[Link](x & " is odd number")
End If
If x = 7 Then
Exit Do
End If
[Link]("value of x is " & x)
x=x+1
Loop
End Sub
Private Sub Button10_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim x = 10
Do
If x Mod 2 = 0 Then
[Link](x & " is even number")
Else
[Link](x & " is odd number")
End If
[Link]("value of x is " & x)
x=x+1
Loop Until x = 10
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = [Link]
End Sub
Private Sub Button11_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'the do while loop skips output 5 and exits when x=7
Dim x = 0
Do While x < 10
x=x+1
If x = 5 Then
Continue Do
End If
[Link]("value of x is " & x)
If x = 7 Then
Exit Do
End If
[Link]("value of x is " & x)
Loop
End Sub
Private Sub About_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
Private Sub Button6_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
Private Sub Button12_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'calculate factorial of 4 using reverse for loop
Dim result As Integer = 4
For j As Integer = result To 1 Step -1
If j = 1 Then
Exit For
Else
result = result * (j - 1)
[Link]("value of result is " & result & " : j is" & j)
End If
Next j
[Link]("factorial of 4 is :" & result)
End Sub
End Class
ListBox
Public Class listbox
Private Sub loops_Load(ByVal sender As [Link], ByVal e As [Link]) Handles
[Link]
For i As Integer = 0 To [Link] - 1
If [Link](i).ToString = [Link] Then
Continue For
End If
[Link]("Name=" & [Link](i).ToString())
Next
'[Link] = False
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]([Link])
End Sub
Private Sub Button3_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link](1, [Link])
End Sub
Private Sub Button4_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link](2) = [Link]
End Sub
Private Sub Button5_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'[Link]([Link])
With ComboBox1
.[Link](.Text)
End With
End Sub
Private Sub Button6_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = 2
End Sub
Private Sub Button7_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
If [Link] = -1 Then
[Link]("Please make a selection")
End If
End Sub
Private Sub Button8_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
Private Sub Button9_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]([Link])
End Sub
End Class
Order Precedence
Public Class order_prec_ch3
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim num1, num2, num3 As Integer
Dim result As Double
' result = [Link]([Link]) / [Link]([Link])
'[Link]("The Result is :" & [Link])
'use BEDMIMAS order of precedence
num1 = 4
num2 = 2
num3 = 3
num1 = [Link]([Link])
num2 = [Link]([Link])
'result = num1 + num2
result = (num1 ^ num2 / num1 * num3) + 5 Mod 2
[Link] = [Link](result)
End Sub
Private Sub ExitBtn_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = [Link]._31
End Sub
End Class
Recursive Function
Public Class recursive_function
Private prod As Integer
Private Function fact(ByVal num As Integer) As Integer
If num = 1 Then
Return 1
Exit Function
Else
prod = num * fact(num - 1)
End If
Return prod
End Function
Private Sub Bt_Cal_Click(ByVal sender As [Link], ByVal e As [Link])
Handles Bt_Cal.Click
Dim input As Integer = [Link](Tb_Input.Text)
Dim result = fact(input)
Tb_Output.Text = result
End Sub
End Class
Form1
Public Class Form1
'this is a module level variable which can be accessed anywhere in the program
Dim Str1String As String = "Greetings! (this is a system event)"
Dim title As String
Private Sub ClearBtn_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
' clearing the textbox value
[Link] = ""
[Link] = [Link]
[Link] = [Link]
[Link] = [Link]
End Sub
Private Sub Form1_Load(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link](Str1String)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = [Link]
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
'scope = local variable which is only available in this event procedure,e.g FnameString
Dim FnameString = [Link]
[Link](FnameString)
[Link]("Goodbye " & title & " " & FnameString)
[Link]()
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = [Link].south_aC
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = [Link].java1
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
End Sub
Private Sub AddCityBtn_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]([Link])
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
title = "Mr"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
title = "Ms"
End Sub
End Class
Practicals
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim ans As Integer = CDbl([Link]) + CDbl([Link])
[Link] = ans
[Link] = "SUM"
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link], [Link]
[Link]()
[Link] = ""
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = CDbl([Link]) - CDbl([Link])
[Link] = "DIFFERENCE"
End Sub
Private Sub Button3_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = CDbl([Link]) * CDbl([Link])
[Link] = "PRODUCT"
End Sub
Private Sub Button4_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link] = FormatNumber(CDbl([Link]) /
CDbl([Link]), 2)
[Link] = "QUOTIENT"
End Sub
Private Sub SimpleAdder_Load(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
End Sub
Prac 2
Option Strict On
Public Class Form1
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim fmtStr As String = "{0,-15}{1,8}"
[Link]()
Dim EmpName As String = Emp_name.Text
Dim EmpNormal As Integer = CInt(Emp_normalhrs.Text)
Dim EmpOverhrs As Integer = CInt(Emp_overhrs.Text)
Dim EmpOvermins As Integer = CInt(Emp_overmins.Text)
Dim Overtime, Tax, Finalsalary As Double
' Dim n As Integer
EmpNormal = EmpNormal * 50
[Link]([Link](fmtStr, "Employee Name", EmpName))
[Link]([Link](fmtStr, "Normal Pay", "R" &
EmpNormal))
Overtime = EmpOverhrs * 75 + (EmpOvermins / 60 * 75)
[Link]([Link](fmtStr, "Overtime Pay", "R" &
FormatNumber(Overtime, 2)))
Tax = 0.15 * (Overtime + EmpNormal)
[Link]([Link](fmtStr, "Tax Deduction", "R" &
FormatNumber(Tax, 2)))
Finalsalary = EmpNormal + Overtime - Tax
[Link]([Link](fmtStr, "Final Salary", "R" &
FormatNumber(Finalsalary, 2)))
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link]()
Emp_name.Clear()
Emp_normalhrs.Clear()
Emp_overhrs.Clear()
Emp_overmins.Clear()
End Sub
End Class
Prac 3
Private numatt, numright As Integer
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim num1, num2, compans, myans As Integer
numatt += 1 'numatt = numatt + 1
Try
num1 = [Link]([Link])
num2 = [Link]([Link])
myans = [Link]([Link])
Catch TheException As FormatException
[Link]("Please enter a valid number")
End Try
If [Link] Then
compans = num1 + num2
ElseIf [Link] Then
compans = num1 * num2
ElseIf [Link] Then
compans = num1 - num2
ElseIf [Link] Then
Try
compans = num1 / num2
Catch TheException As ArithmeticException
[Link]("Cannot divide by 0: " &
[Link])
End Try
End If
[Link] = [Link](compans)
If myans = compans Then
[Link]("You are correct")
numright += numright
Else
[Link]("You are incorrect")
End If
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link]()
[Link]()
[Link]()
[Link]()
End Sub
Private Sub Button3_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim message As String = "you have attempted: " & numatt & " and have:
" & numright & " correct."
[Link](message)
[Link]()
End Sub
Prac 3 Add Student
Public Class AddStudent
Private Sub AddStudent_Load(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
End Sub
Private Function ValidateID(ByVal MyId As String) As Boolean
Dim a As Integer
Dim b1 As String, b2 As Integer
Dim c1 As String, c2 As Integer
Dim d As Integer
Dim FinalBit As Integer
For J As Integer = 0 To [Link] - 2 Step 2
a += [Link](MyId(J))
Next
For J As Integer = 1 To [Link] - 2 Step 2
b1 &= MyId(J)
Next
b2 = [Link](b1) * 2
c1 = [Link]
For J As Integer = 0 To [Link] - 1
c2 += [Link](c1(J))
Next
d = a + c2
FinalBit = 10 - (d Mod 10)
If FinalBit = [Link](MyId([Link] - 1)) Then
Return True
Else
Return False
End If
End Function
Private Sub MaskedTextBox1_Leave(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim Valid As Boolean
Dim MyYear, MyMonth, MyDate As Integer
Dim ThisID As String = [Link]
If [Link] = 13 Then
MyMonth = CInt([Link](2, 2))
If MyMonth >= 1 And MyMonth <= 12 Then
MyDate = CInt([Link](4, 2))
If MyDate >= 1 And MyDate <= 31 Then
Valid = ValidateID(ThisID)
End If
End If
End If
If Not (Valid) Then
[Link]("ID number is not valid", "Error", [Link],
[Link])
[Link]()
[Link]()
Else
MyYear = CInt([Link](0, 2))
If MyYear <= 14 Then
Dim MyCentury As Integer = InputBox("Enter 19 or 20")
MyYear = [Link] & MyYear
Else
MyYear = "19" & MyYear
End If
Dim Age As Integer = [Link] - MyYear
If [Link] < MyMonth Then
Age -= 1
End If
[Link] = Age
[Link] = [Link](4, 2) & "/" & [Link](2, 2) &
"/" & MyYear
If [Link](6, 1) >= 5 Then
[Link] = "Male"
Else
[Link] = "Female"
End If
End If
End Sub
End Class
Prac 4
Private Function GetMyName() As String
Dim MyName As String = InputBox("Enter Name of Player")
Return MyName
End Function
Private Sub Button5_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = GetMyName()
End Sub
Private Sub ChangeFontColourToolStripMenuItem_Click(ByVal sender As [Link],
ByVal e As [Link]) Handles [Link]
[Link]()
[Link] = [Link]
End Sub
Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As [Link], ByVal e
As [Link]) Handles [Link]
[Link]()
[Link] = [Link]
End Sub
Prac 4 TicTacToe
Public Class Game2
'Counts the number of moves - Maximum = 9
Dim Moves As Integer = 0
'These represent the positions on the board
Dim A1, A2, A3 As Integer
Dim B1, B2, B3 As Integer
Dim C1, C2, C3 As Integer
'If Turn1 is true, then X to play, If Turn2 is true then O to Play
Dim Turn1 As Boolean = True
Dim Turn2 As Boolean = False
Private Sub WhosTurn()
If Turn1 = True Then
MsgBox("X Turn to play")
ElseIf Turn2 = True Then
MsgBox("O Turn to play")
End If
End Sub
Private Sub CheckForWin()
'X Across
If (A1 = 1 And A2 = 1 And A3 = 1) Or (B1 = 1 And B2 = 1 And B3 = 1) Or (C1 = 1
And C2 = 1 And C3 = 1) Then
MsgBox("X Wins!!!")
'X Down
ElseIf (A1 = 1 And B1 = 1 And C1 = 1) Or (A2 = 1 And B2 = 1 And C2 = 1) Or (A3
= 1 And B3 = 1 And C3 = 1) Then
MsgBox("X Wins!!!")
'X Diagonal
ElseIf (A1 = 1 And B2 = 1 And C3 = 1) Or (A3 = 1 And B2 = 1 And C1 = 1) Then
MsgBox("X Wins!!!")
'O Across
ElseIf (A1 = 2 And A2 = 2 And A3 = 2) Or (B1 = 2 And B2 = 2 And B3 = 2) Or (C1
= 2 And C2 = 2 And C3 = 2) Then
MsgBox("O Wins!!!")
'O Down
ElseIf (A1 = 2 And B1 = 2 And C1 = 2) Or (A2 = 2 And B2 = 2 And C2 = 2) Or (A3
= 2 And B3 = 2 And C3 = 2) Then
MsgBox("O Wins!!!")
'O Diagonal
ElseIf (A1 = 2 And B2 = 2 And C3 = 2) Or (A3 = 2 And B2 = 2 And C1 = 2) Then
MsgBox("O Wins!!!")
ElseIf Moves = 9 Then
MsgBox("Draw")
End If
End Sub
Private Sub Game2_Load(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
WhosTurn()
End Sub
'Other picture boxes will have code similar to below
Private Sub PictureBox1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
If Turn1 Then
A1 = 1
[Link] = [Link]
[Link] = [Link]
Turn1 = False
Turn2 = True
WhosTurn()
Else
A1 = 2
[Link] = [Link]
[Link] = [Link]
Turn1 = True
Turn2 = False
WhosTurn()
End If
[Link] = False
Moves += 1
CheckForWin()
End Sub
Pizza System
Dim StandardPizza As Integer = 30
Variable declarations required to keep track of subtotal, total cost etc.
Dim ToppingsCost As Integer = 0
Dim CheeseCost As Integer = 0
Dim TotalCost As Integer = 0
Dim PizzaCost As Integer = 0
Dim SubTotal As Integer = 0
Could also be hard-coded
Dim PepperoniCost As Integer = 5
Dim MushroomCost As Integer = 4
Dim AnchoviesCost As Integer = 8
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
PizzaCost = StandardPizza
Dim Toppings As String = ""
Price changes depending on toppings chosen. String variable keeps track
for output purposes
If [Link] Then
PizzaCost += PepperoniCost
Toppings &= "Pepperoni "
End If
If [Link] Then
PizzaCost += MushroomCost
Toppings &= "Mushroom "
End If
If [Link] Then
PizzaCost += AnchoviesCost
Toppings &= "Anchovies "
End If
Dim CheeseReqs As String = ""
If [Link] Then
PizzaCost += 5
CheeseReqs = "Extra Cheese "
ElseIf [Link] Then
PizzaCost += 15
CheeseReqs = "Cheesy Crust "
End If
Calculation of subtotal and grand total
SubTotal = [Link] * PizzaCost
TotalCost += SubTotal
Preperation of output
Dim Output As String = [Link] & " X Pizza " & Toppings &
CheeseReqs
Creating two zones, 1 with length 60 for pizza and 1 for price
Dim MyStringFormat As String = "{0, -60}{1, 8}"
Output of pizza ordered with price using zoning
[Link]([Link](MyStringFormat, Output,
[Link]("C")))
Reset pizza price in preparation for another possible pizza order
SubTotal = 0
PizzaCost = 0
End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link]("Final Cost = " & [Link]("C"))
End Sub
End Class