IS Coding
IS Coding
IS Coding
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
Console.WriteLine("Welcome User! Today is: " & Date.Today() & vbCrLf)
'capture input from user
Console.WriteLine("Please Enter a full name")
Full_name = Console.ReadLine()
Console.WriteLine("Please enter Date of Birth in 'dd/Mon/yy' format")
'local variable declaration
Dim DOB As Date = Convert.ToDateTime(Console.ReadLine()) 'or CDate(string value)
Age = Date.Today.Year - DOB.Year
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 + Sum.ToString
Console.WriteLine("name: " & Full_name & " Age: " & Age & " Password: " & Password)
Console.WriteLine("Do you want to Continue with another password creation")
ans = Console.ReadLine()
Loop
Catch ex As FormatException
Console.WriteLine("Format Exception: " & ex.Message)
Catch ex As Exception
Console.WriteLine("General Exception: " & ex.Message)
End Try
End Sub
End Module
Decision Loops
Public NotInheritable Class AboutBox2
With FontDialog1
FontDialog1.Font = Me.Font
.ShowDialog()
'apply new selected font
Me.Font = .Font
End With
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
ToolStripStatusLabel1.Text = "This form demonstrates functions, procedures and menus "
End Sub
Private Sub OpenRegistrationForrmToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles OpenRegistrationForrmToolStripMenuItem.Click
FirstName = TextBox1.Text
Reg_Form.Activate()
Reg_Form.Show()
Me.Hide()
End Sub
Private Sub CloseAllFormsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CloseAllFormsToolStripMenuItem.Click
Reg_Form.Close()
Me.Close()
End Sub
Private Sub PickUrFavToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PickUrFavToolStripMenuItem.Click
'calling a sub procedure to change background color
Call CustomiseBack()
End Sub
Private Sub ChangeFormTextColorToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ChangeFormTextColorToolStripMenuItem.Click
'call to sub procedure to customise text color
Call CustomiseTxT()
End Sub
Private Sub ExitToolStripMenuItem1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ExitToolStripMenuItem1.Click
MsgBox("Saying GoodBye!!")
Reg_Form.Close()
Me.Close()
End Sub
Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles HelpToolStripMenuItem.Click
MsgBox("Sorry Work in Progress")
End Sub
Private Sub CloseRegistrationFormToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CloseRegistrationFormToolStripMenuItem.Click
Reg_Form.Close()
End Sub
Private Sub Comp_ByVal_ByRef_Btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Comp_ByVal_ByRef_Btn.Click
Error Handling
Public Class Error_Handling
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' generates arithmatic exception on Integer Division (\) NOT in normal division (/)
Dim result As Double = num1 \ num2
TextBox3.Text = result
Catch ex As ArithmeticException
MessageBox.Show("Arithmatic exception: " & ex.Message)
Catch ex As InvalidCastException
MessageBox.Show("Invalid Cast Exception occured: (this is your own customised message
)" & ex.Message)
Catch ex As Exception
MessageBox.Show("General Exception: " & ex.Message)
Finally
MessageBox.Show("This block called 'Finally' is executed in the very end EVEN if NO
exception has occured." & _
Environment.NewLine & "SO WISHING YOU GOODBYE ---THE END------")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
TextBox1.Text = String.Empty
TextBox2.Text = ""
TextBox3.Clear()
End Sub
End Class
Select Case
Public Class Ch4_select_case
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
'Try
'Dim marks As Double = Double.Parse(TextBox1.Text)
Dim marks As Double = InputBox("Enter Marks %age")
Select Case marks
Case 75 To 100
MessageBox.Show("Distinction")
Case 60 To 74
MessageBox.Show("Credit")
Case 40 To 59
MessageBox.Show("Pass")
Case 0 To 40 ' or case Is < 40
MessageBox.Show("Fail")
Case Else
MessageBox.Show("Wrong %age entered")
End Select
'Catch TheException As FormatException
'MessageBox.Show("please input numbers only")
'Catch TheException As InvalidCastException
'MessageBox.Show("invalid entry")
'End Try
End Sub
Private Sub Ch4_select_case_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
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 System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
'Do While flag = True
dr = DialogResult.Yes
Do While dr = DialogResult.Yes
NoOfOrders = InputBox("Enter No of Orders")
GrandTotalDecimal = InputBox("Enter Total Sales")
AvgDecimal = GrandTotalDecimal / NoOfOrders
'create multiline formatted message string
MessageString = " Number of Orders : " & NoOfOrders.ToString("N0") &
Environment.NewLine & " Average Sales: " & AvgDecimal.ToString("C")
'display messagebox with selected buttons and icon
MessageBox.Show(MessageString, "Sales Summery", MessageBoxButtons.OKCancel,
MessageBoxIcon.Information)
dr = MessageBox.Show("Would you like to do another calculation?", "Enter your choice",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Debug.Print(" you clicked " & dr)
If dr = DialogResult.No Then
Exit Do
End If
'Debug.Print("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 System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim age As Integer = TextBox1.Text
If age < 18 Then
MessageBox.Show("Minor")
Else
MessageBox.Show("Adult")
End If
End Sub
End If
If x = 7 Then
Exit Do
End If
Debug.WriteLine("value of x is " & x)
x=x+1
Loop
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button10.Click
Dim x = 10
Do
If x Mod 2 = 0 Then
MessageBox.Show(x & " is even number")
Else
MessageBox.Show(x & " is odd number")
End If
Debug.WriteLine("value of x is " & x)
x=x+1
Loop Until x = 10
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = TextBox2.Text.ToUpper
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button11.Click
'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
MessageBox.Show("value of x is " & x)
If x = 7 Then
Exit Do
End If
Debug.WriteLine("value of x is " & x)
Loop
End Sub
Private Sub About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles About.Click
AboutBox1.Show()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button6.Click
Me.Close()
End Sub
ListBox
Public Class listbox
Private Sub loops_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
For i As Integer = 0 To NameListBox.Items.Count - 1
If NameListBox.Items(i).ToString = String.Empty Then
Continue For
End If
Debug.WriteLine("Name=" & NameListBox.Items(i).ToString())
Next
'TextBox1.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
NameListBox.Items.Add(TextBox1.Text)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
NameListBox.Items.Insert(1, TextBox1.Text)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
NameListBox.Items(2) = TextBox1.Text
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
'ComboBox1.Items.Add(ComboBox1.Text)
With ComboBox1
.Items.Add(.Text)
End With
End Sub
Order Precedence
Public Class order_prec_ch3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim num1, num2, num3 As Integer
Dim result As Double
' result = Double.Parse(TextBox1.Text) / Convert.ToDouble(TextBox2.Text)
'MessageBox.Show("The Result is :" & result.ToString)
'use BEDMIMAS order of precedence
num1 = 4
num2 = 2
num3 = 3
num1 = Integer.Parse(TextBox1.Text)
num2 = Decimal.Parse(TextBox2.Text)
'result = num1 + num2
result = (num1 ^ num2 / num1 * num3) + 5 Mod 2
TextBox3.Text = Convert.ToString(result)
End Sub
Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles ExitBtn.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
PictureBox1.Image = My.Resources._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 System.Object, ByVal e As System.EventArgs)
Handles Bt_Cal.Click
Dim input As Integer = Integer.Parse(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 System.Object, ByVal e As System.EventArgs)
Handles ClearBtn.Click
' clearing the textbox value
FnameTxt.Text = ""
surnameTxt.Text = String.Empty
TextBox1.Text = String.Empty
TextBox2.Text = String.Empty
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
MessageBox.Show(Str1String)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox2.Text = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
'scope = local variable which is only available in this event procedure,e.g FnameString
Dim FnameString = FnameTxt.Text
Debug.Print(FnameString)
MessageBox.Show("Goodbye " & title & " " & FnameString)
Me.Close()
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton3.CheckedChanged
PictureBox1.Image = My.Resources.south_aC
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton4.CheckedChanged
PictureBox1.Image = My.Resources.java1
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub AddCityBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles AddCityBtn.Click
CityCombo.Items.Add(CityCombo.Text)
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton1.CheckedChanged
title = "Mr"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton2.CheckedChanged
title = "Ms"
End Sub
End Class
Practicals
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ans As Integer = CDbl(TextBox1.Text) + CDbl(TextBox2.Text)
TextBox3.Text = ans
Label3.Text = "SUM"
End Sub
Prac 2
Option Strict On
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fmtStr As String = "{0,-15}{1,8}"
ListBox1.Items.Clear()
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
ListBox1.Items.Add(String.Format(fmtStr, "Employee Name", EmpName))
ListBox1.Items.Add(String.Format(fmtStr, "Normal Pay", "R" &
EmpNormal))
Overtime = EmpOverhrs * 75 + (EmpOvermins / 60 * 75)
ListBox1.Items.Add(String.Format(fmtStr, "Overtime Pay", "R" &
FormatNumber(Overtime, 2)))
Prac 3
Private numatt, numright As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim num1, num2, compans, myans As Integer
numatt += 1 'numatt = numatt + 1
Try
num1 = Integer.Parse(TextBox1.Text)
num2 = Integer.Parse(TextBox2.Text)
myans = Integer.Parse(TextBox3.Text)
Catch TheException As FormatException
MessageBox.Show("Please enter a valid number")
End Try
If RadioButton1.Checked Then
compans = num1 + num2
ElseIf RadioButton2.Checked Then
compans = num1 * num2
ElseIf RadioButton3.Checked Then
compans = num1 - num2
ElseIf RadioButton4.Checked Then
Try
compans = num1 / num2
Catch TheException As ArithmeticException
MessageBox.Show("Cannot divide by 0: " &
TheException.Message)
End Try
End If
TextBox4.Text = Convert.ToString(compans)
If myans = compans Then
MessageBox.Show("You are correct")
numright += numright
Else
Return False
End If
End Function
Private Sub MaskedTextBox1_Leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MaskedTextBox1.Leave
Dim Valid As Boolean
Dim MyYear, MyMonth, MyDate As Integer
Dim ThisID As String = MaskedTextBox1.Text
If ThisID.Length = 13 Then
MyMonth = CInt(ThisID.Substring(2, 2))
If MyMonth >= 1 And MyMonth <= 12 Then
MyDate = CInt(ThisID.Substring(4, 2))
If MyDate >= 1 And MyDate <= 31 Then
Valid = ValidateID(ThisID)
End If
End If
End If
If Not (Valid) Then
MessageBox.Show("ID number is not valid", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
MaskedTextBox1.Clear()
MaskedTextBox1.Focus()
Else
MyYear = CInt(ThisID.Substring(0, 2))
If MyYear <= 14 Then
Dim MyCentury As Integer = InputBox("Enter 19 or 20")
MyYear = MyCentury.ToString & MyYear
Else
MyYear = "19" & MyYear
End If
Dim Age As Integer = Date.Today.Year - MyYear
If Date.Today.Month < MyMonth Then
Age -= 1
End If
TextBox4.Text = Age
MaskedTextBox2.Text = ThisID.Substring(4, 2) & "/" & ThisID.Substring(2, 2) &
"/" & MyYear
If ThisID.Substring(6, 1) >= 5 Then
TextBox3.Text = "Male"
Else
TextBox3.Text = "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
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 System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
WhosTurn()
End Sub
'Other picture boxes will have code similar to below
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click
If Turn1 Then
A1 = 1
PictureBox1.Image = My.Resources.imagesX
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Turn1 = False
Turn2 = True
WhosTurn()
Else
A1 = 2
PictureBox1.Image = My.Resources.imagesO
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Turn1 = True
Turn2 = False
WhosTurn()
End If
PictureBox1.Enabled = 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 System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
PizzaCost = StandardPizza
Dim Toppings As String = ""
Price changes depending on toppings chosen. String variable keeps track