Вы находитесь на странице: 1из 23

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
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

'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 System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
' Set the title of the form.
Dim ApplicationTitle As String
If My.Application.Info.Title <> "" Then
ApplicationTitle = My.Application.Info.Title
Else
ApplicationTitle =
System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
End If
Me.Text = String.Format("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).
Me.LabelProductName.Text = My.Application.Info.ProductName
Me.LabelVersion.Text = String.Format("Version {0}", My.Application.Info.Version.ToString)
Me.LabelCopyright.Text = My.Application.Info.Copyright
Me.LabelCompanyName.Text = My.Application.Info.CompanyName
Me.TextBoxDescription.Text = My.Application.Info.Description
End Sub
Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles OKButton.Click
Me.Close()
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
myColor.ShowDialog()
Me.BackColor = myColor.Color
End Sub
'defining a sub procedure
Private Sub CustomiseTxT()
myColor = New ColorDialog
myColor.ShowDialog()
Me.ForeColor = myColor.Color
End Sub
Private Sub CloseRegFormToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CloseRegFormToolStripMenuItem.Click
Reg_Form.Close()
End Sub
Private Sub ExitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Reg_Form.Close()
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
input = Integer.Parse(InputBox("Enter a Number", "Recursion"))
'call to function called fact
Dim result As Integer = fact(input)
MessageBox.Show("Factorial : " & result)
MessageBox.Show("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 System.Object, ByVal e As System.EventArgs)


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 System.Object, ByVal e As System.EventArgs)
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 System.Object, ByVal e As
System.EventArgs) Handles ByRef_Btn.MouseHover
ToolStripStatusLabel1.Text = "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 System.Object, ByVal e As
System.EventArgs) Handles ByVal_Btn.MouseHover
ToolStripStatusLabel1.Text = "This Button demonstrates ByVal usage in a sub-procedure"
End Sub

Private Sub ChangeBackColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles ChangeBackColorToolStripMenuItem.Click
With myColor
.Color = Me.BackColor
.ShowDialog()
Me.BackColor = .Color
End With
End Sub
Private Sub ChangeForeColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ChangeForeColorToolStripMenuItem.Click
With myColor
.Color = Me.ForeColor
.ShowDialog()
Me.ForeColor = .Color
End With
End Sub
Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ChangeFontToolStripMenuItem.Click
myFont.Font = Me.Font

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

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 & Environment.NewLine & "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 System.Object, ByVal e As
System.EventArgs) Handles HelpToolStripMenuItem1.Click
myFont = New FontDialog
myFont.ShowDialog()
Me.Font = myFont.Font
End Sub
Private Sub PickYoursToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PickYoursToolStripMenuItem.Click
CustomiseTxT()
End Sub
End Class

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

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
TextBox2.Text = TextBox2.Text.ToUpper()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
TextBox2.Text = TextBox2.Text.ToLower()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
'showing nested if statements
Dim temp = TextBox3.Text
If temp <= 32 Then
Label3.Text = "freezing"
ElseIf temp > 80 Then
Label3.Text = "hot"
Else
Label3.Text = "moderate"
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
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
Debug.Print(Sum)
Next J
MessageBox.Show("Average = " & Sum / 4)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button7.Click
For J As Integer = 0 To 5
For K As Integer = 0 To 5
T1.Text = T1.Text & K
Next K
T1.Text = T1.Text & ControlChars.NewLine
Next J
End Sub
Private Sub FAct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
FAct.Click
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

MessageBox.Show("factorial :" & prod)


'Dim result As Integer = x
'For j As Integer = 1 To result
'If j = 1 Then
'Exit For
'Else
'result = result * (j)
'Debug.Print("result is " & result & "j is" & j)
'End If
'Next j
End Sub
Private Sub oddEven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles oddEven.Click
For x = 1 To 10
If x = 5 Then
Continue For
End If
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)
'Exit For
Next x
End Sub
Private Sub if_then_else_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'TextBox1.ReadOnly = True
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button8.Click
If man.Text > mari.Text Then
MessageBox.Show(Label4.Text)
ElseIf man.Text < mari.Text Then
MessageBox.Show(Label5.Text)
Else
MessageBox.Show("Draw")
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button9.Click
Dim x = 1
Do While x < 10
'If x Mod 2 = 0 Then
If x = 5 Then
Continue Do
'End If
MessageBox.Show(x & " is even number")
Else
MessageBox.Show(x & " is odd number")

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

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button12.Click
'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)
Debug.Print("value of result is " & result & " : j is" & j)
End If
Next j
MessageBox.Show("factorial of 4 is :" & result)
End Sub
End Class

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

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button6.Click
NameListBox.SelectedIndex = 2
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button7.Click
If NameListBox.SelectedIndex = -1 Then
MessageBox.Show("Please make a selection")
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button8.Click
NameListBox.Items.Clear()
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button9.Click
ComboBox1.Items.Clear()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
ComboBox2.Items.Add(ComboBox2.Text)
End Sub
End Class

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

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
TextBox3.Clear()
Label3.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox3.Text = CDbl(TextBox1.Text) - CDbl(TextBox2.Text)
Label3.Text = "DIFFERENCE"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
TextBox3.Text = CDbl(TextBox1.Text) * CDbl(TextBox2.Text)
Label3.Text = "PRODUCT"
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
TextBox3.Text = FormatNumber(CDbl(TextBox1.Text) /
CDbl(TextBox2.Text), 2)
Label3.Text = "QUOTIENT"
End Sub
Private Sub SimpleAdder_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
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)))

Tax = 0.15 * (Overtime + EmpNormal)


ListBox1.Items.Add(String.Format(fmtStr, "Tax Deduction", "R" &
FormatNumber(Tax, 2)))
Finalsalary = EmpNormal + Overtime - Tax
ListBox1.Items.Add(String.Format(fmtStr, "Final Salary", "R" &
FormatNumber(Finalsalary, 2)))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
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 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

MessageBox.Show("You are incorrect")


End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim message As String = "you have attempted: " & numatt & " and have:
" & numright & " correct."
MessageBox.Show(message)
Me.Close()
End Sub

Prac 3 Add Student


Public Class AddStudent

Private Sub AddStudent_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
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 MyId.Length - 2 Step 2
a += Integer.Parse(MyId(J))
Next
For J As Integer = 1 To MyId.Length - 2 Step 2
b1 &= MyId(J)
Next
b2 = Integer.Parse(b1) * 2
c1 = b2.ToString
For J As Integer = 0 To c1.Length - 1
c2 += Integer.Parse(c1(J))
Next
d = a + c2
FinalBit = 10 - (d Mod 10)
If FinalBit = Integer.Parse(MyId(MyId.Length - 1)) Then
Return True
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

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button5.Click
GroupBox1.Text = GetMyName()
End Sub
Private Sub ChangeFontColourToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ChangeFontColourToolStripMenuItem.Click
ColorDialog1.ShowDialog()
GroupBox1.ForeColor = ColorDialog1.Color
End Sub
Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ChangeFontToolStripMenuItem.Click
FontDialog1.ShowDialog()
GroupBox1.Font = FontDialog1.Font
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 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

for output purposes


If CheckBox1.Checked Then
PizzaCost += PepperoniCost
Toppings &= "Pepperoni "
End If
If CheckBox2.Checked Then
PizzaCost += MushroomCost
Toppings &= "Mushroom "
End If
If CheckBox3.Checked Then
PizzaCost += AnchoviesCost
Toppings &= "Anchovies "
End If
Dim CheeseReqs As String = ""
If RadioButton2.Checked Then
PizzaCost += 5
CheeseReqs = "Extra Cheese "
ElseIf RadioButton3.Checked Then
PizzaCost += 15
CheeseReqs = "Cheesy Crust "
End If
Calculation of subtotal and grand total
SubTotal = MaskedTextBox1.Text * PizzaCost
TotalCost += SubTotal
Preperation of output
Dim Output As String = MaskedTextBox1.Text & " 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
ListBox1.Items.Add(String.Format(MyStringFormat, Output,
SubTotal.ToString("C")))
Reset pizza price in preparation for another possible pizza order
SubTotal = 0
PizzaCost = 0
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ListBox1.Items.Add("Final Cost = " & TotalCost.ToString("C"))
End Sub
End Class

Вам также может понравиться