Instructions to copy this code: CTL+A to select all, CTL+C to copy, then open Notepad and press CTL+V to paste all the code. ___________________________________________________________________________ Source code for "Programming: A Beginner's Guide" ____________________________________________________________________________ Chapter 1 For number = 1 To 20 Debug.Print number Next number for (number=1; number<=20; number++) { printf(" %d\n", number); } ____________________________________________________________________________ Chapter 2 Option Strict Off Option Explicit Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load answer = InputBox("Please type in the delay in minutes...") MsgBox(answer) End End Sub End Class Option Strict Off Option Explicit Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load EndTime = InputBox("Please type in the delay in minutes...") EndTime = EndTime + Minute(Now) Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop MsgBox("Time's Up!") End End Sub End Class ____________________________________________________________________________ Chapter 3 MsgBox("This is my prompt", , "This is the title") MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical Or MsgBoxStyle.YesNo EndTime = InputBox("Please type in the delay in minutes...") EndTime = EndTime + Minute(Now) EndTime = InputBox("Please type in the delay in minutes...") EndTime = EndTime + Minute(Now) MsgBox(EndTime) Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop MsgBox("Time's Up!") ____________________________________________________________________________ Chapter 4 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Interval = NumericUpDown1.Value * 60000 Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Timer1.Stop() MsgBox("Time's Up.") End End Sub Option Strict Off Option Explicit Off Public Class Form1 Dim Counter, TotalSeconds Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Interval = 1000 TotalSeconds = NumericUpDown1.Value * 60 ProgressBar1.Maximum = TotalSeconds Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Counter = Counter + 1 ProgressBar1.Value = Counter If Counter >= TotalSeconds Then Timer1.Stop() MsgBox("Time's up.") End End If End Sub End Class ____________________________________________________________________________ Chapter 5 MyFoodMoney = 212 MsgBox(MyFoodMoney) Dim MyFoodMoney Dim MyFoodMoney = 32 Dim MyFoodMoney As Integer Dim MyFoodMoney As Integer = 212 Strict Off Option Explicit Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Num1 = 14 Num2 = 7 MsgBox(Num1 + Num2) End Sub End Class Str1 = "Sam " Str2 = "Rockwell" MsgBox(Str1 & Str2) Num1 = 14 Num2 = 6 If Num1 > Num2 Then MsgBox("14 wins") End If If 5 + 2 = 4 Or 6 + 6 = 12 Then MsgBox ("One of them is true.") If 5 + 2 = 4 And 6 + 6 = 12 Then MsgBox("Both of them are true.") If Num1 > Num2 Then MsgBox("14 wins") End If If 5 + 2 = 4 And 6 + 6 = 12 Then MsgBox("Both of them are true.") End If Option Strict Off Option Explicit Off Public Class Form1 Dim Counter, TotalSeconds Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Interval = 1000 TotalSeconds = NumericUpDown1.Value * 60 ProgressBar1.Maximum = TotalSeconds Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Counter = Counter + 1 ProgressBar1.Value = Counter If Counter >= TotalSeconds Then Timer1.Stop() MsgBox("Time's up.") End End If End Sub End Class Option Strict Off Option Explicit Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyNumber = 3 AddTwo() End Sub Sub AddTwo() MsgBox(2 + MyNumber) End Sub End Class Option Strict Off Option Explicit Off Public Class Form1 Dim MyNumber = 3 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddTwo() End Sub Sub AddTwo() MsgBox(2 + MyNumber) End Sub End Class Option Strict Off Option Explicit Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyNumber = 3 AddTwo(MyNumber) End Sub Sub AddTwo(ByVal WhatTheySent) MsgBox(2 + WhatTheySent) End Sub End Class MyNumber = 3 AddTwo(MyNmber) MsgBox(CStr(MyNumber)) MsgBox("9" + "10") Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyNumber = InputBox("Please type in a number.") End Sub End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyNumber MyNumber = InputBox("Please type in a number.") End Sub MyNumber = CInt(InputBox("Please type in a number.")) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyString Dim MyNumber MyString = "22" MyNumber = 44 MsgBox(MyString & MyNumber) End Sub ____________________________________________________________________________ Chapter 6 Dim FirstQuestion = "Who painted the Mona Lisa?" Dim SecondQuestion = "In what city is the Mona Lisa displayed?" Dim ThirdQuestion = "Was the Mona Lisa painted before 300 AD?" Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrQuestions(3) arrQuestions(0) = "Who painted the Mona Lisa?" arrQuestions(1) = "In what city is the Mona Lisa displayed?" arrQuestions(2) = "Was the Mona Lisa painted before 300 AD?" MsgBox(arrQuestions(1)) End End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Total Dim JanFood = 90 Dim FebFood = 122 Dim MarFood = 125 Dim AprFood = 78 Dim MayFood = 144 Dim JneFood = 89 Dim JulFood = 90 Dim AugFood = 140 Dim SeptFood = 167 Dim OctFood = 123 Dim NovFood = 133 Dim DecFood = 125 Total = JanFood + FebFood + MarFood + AprFood + MayFood + JneFood + JulFood + AugFood + SeptFood + OctFood + NovFood + DecFood MsgBox(Total) End End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrMonthFoodBill(11) Dim Total arrMonthFoodBill(0) = 90 arrMonthFoodBill(1) = 122 arrMonthFoodBill(2) = 125 arrMonthFoodBill(3) = 78 arrMonthFoodBill(4) = 144 arrMonthFoodBill(5) = 89 arrMonthFoodBill(6) = 90 arrMonthFoodBill(7) = 140 arrMonthFoodBill(8) = 167 arrMonthFoodBill(9) = 123 arrMonthFoodBill(10) = 133 arrMonthFoodBill(11) = 125 For i = 0 To 11 Total = Total + arrMonthFoodBill(i) Next i MsgBox(Total) End Sub End Class Dim MonthFoodBill(11) MonthFoodBill(0) = 90 For i = 0 To 11 For i = 0 To UBound(MonthFoodBill) Dim arrQuestions() Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrQuestions() Dim Answer Answer = Input box("How many questions do you want in this quiz?") ReDim arrQuestions(Answer - 1) MsgBox(UBound(arrQuestions)) End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrQuestions(3) Dim arrAnswers(3) Dim Response Dim Counter 'Holds the number of correct answers arrQuestions(0) = "Was the Mona Lisa painted by Leonardo?" arrAnswers(0) = 1 arrQuestions(1) = "Is the Mona Lisa displayed in a museum in Rome?" arrAnswers(1) = 0 arrQuestions(2) = "Was the Mona Lisa painted before 300 AD?" arrAnswers(2) = 0 arrQuestions(3) = "Is the Mona Lisa also known as La Gioconda?" arrAnswers(3) = 1 For i = 0 To UBound(arrQuestions) Response = MsgBox(arrQuestions(i), MsgBoxStyle.YesNo, "Question " & (i)) 'THEY CLICKED THE YES BUTTON (6 = Yes button): ' Answer = 1 means True (yes) so they are correct: If Response = vbYes And arrAnswers(i) = 1 Then MsgBox("Correct") Counter = Counter + 1 End If ' answer = 0 False, so they are wrong: If Response = vbYes And arrAnswers(i) = 0 Then MsgBox("Wrong") End If 'THEY CLICKED THE NO BUTTON (7 = No button): ' Answer = 0 means False (no) so they are correct: If Response = vbNo And arrAnswers(i) = 0 Then MsgBox("Correct") Counter = Counter + 1 End If ' Answer = 1 (True) so they are wrong: If Response = vbNo And arrAnswers(i) = 1 Then MsgBox("Wrong") End If Next MsgBox("You got " & Counter & " correct out of " & UBound(arrAnswers) + 1 & " questions.") End End Sub End Class Dim arrQuestions(3) Dim arrAnswers(3) Dim Response Dim Counter 'Holds the number of correct answers arrQuestions(0) = "Was the Mona Lisa painted by Leonardo?" arrAnswers(0) = 1 arrQuestions(1) = "Is the Mona Lisa displayed in a museum in Rome?" arrAnswers(1) = 0 arrQuestions(2) = "Was the Mona Lisa painted before 300 AD?" arrAnswers(2) = 0 arrQuestions(3) = "Is the Mona Lisa also known as La Gioconda?" arrAnswers(3) = 1 For i = 0 To UBound(arrQuestions) Response = MsgBox(arrQuestions(i), MsgBoxStyle.YesNo, "Question " & (i + 1)) Response = MsgBox(arrQuestions(i), MsgBoxStyle.YesNo, "Question " & (i + 1) & " of " & UBound(arrQuestions) + 1) If Response = vbYes And arrAnswers(i) = 1 Then MsgBox("Correct") Counter = Counter + 1 End If If Response = vb Dim Counter 'Holds the number of correct answers ' Answer = 1 means True (yes) so they are correct: If Response = vbYes And arrAnswers(i) = 1 Then Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrTest() = {1, 5, 6, 2, 66} For i = 0 To UBound(arrTest) Debug.WriteLine(arrTest(i)) Next End End Sub End Class arrQuestions(0) = "Was the Mona Lisa painted by Leonardo?" arrAnswers(0) = 1 arrQuestions(1) = "Is the Mona Lisa displayed in a museum in Rome?" arrAnswers(1) = 0 Debug.WriteLine(arrTest(i)) Dim arrTest() = {1, 5, 6, 2, 66} Array. For i = 0 To UBound(arrTest) Array.Sort(arrTest) Array.Reverse(arrTest) ____________________________________________________________________________ Chapter 7 loop and eliminate the repetitions. For example, this code Debug.Write(1) Debug.Write(2) Debug.Write(3) Debug.Write(4) For i = 1 To 4 Debug.Write(i) Next For i = 1 to 4 Next i Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Countdown For i = 1 To 4 Debug.Write(i & ",") Next Debug.WriteLine(" ") Countdown = InputBox("Please enter the number of times you want to do this task") Do While Countdown > 0 Debug.Write("Looping" & ",") Countdown -= 1 Loop End End Sub End Class Countdown = Countdown - 1 For i = 4 To 1 Step -1 Debug.Write(i) Next For i = 12 To 24 Step 3 Debug.Write(i & ", ") Next Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyArray(200) MyArray(72) = "Clovis" For i = 0 To UBound(MyArray) If MyArray(i) = "Clovis" Then Debug.Write("Clovis found at index number: " & i) Exit For End If Next i End End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s, i Dim MyArray(200) MyArray(72) = "Clovis" Do While s <> "Clovis" s = MyArray(i) If MyArray(i) = "Clovis" Then Debug.Write("Clovis found at index number: " & i) End If i += 1 Loop End End Sub End Class For i = 0 To UBound(MyArray) If MyArray(i) = "Clovis" Then Debug.Write("Clovis found at index number: " & i) Exit For End If Next i Do While s <> "Clovis" For i = 1 To 3 For j = 1 To 10 Next j Next i Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i, j For i = 1 To 5 For j = 1 To 10 Debug.Write(j) Next j Debug.WriteLine(" ") Next i End End Sub End Class Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop Do CurrentTime = Minute(Now) Loop While CurrentTime < EndTime Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop Do Until CurrentTime = EndTime CurrentTime = Minute(Now) Loop ____________________________________________________________________________ Chapter 8 If Counter >= TotalSeconds Then Timer1.Stop() MsgBox("Time's up.") End End If If Response = vbYes And arrAnswers(i) = 1 Then MsgBox("Correct") Counter = Counter + 1 End If ' answer = 0 False, so they are wrong: If Response = vbYes And arrAnswers(i) = 0 Then MsgBox("Wrong") End If 'THEY CLICKED THE NO BUTTON (7 = No button): ' Answer = 0 means False (no) so they are correct: If Response = vbNo And arrAnswers(i) = 0 Then MsgBox("Correct") Counter = Counter + 1 End If ' Answer = 1 (True) so they are wrong: If Response = vbNo And arrAnswers(i) = 1 Then MsgBox("Wrong") End If If X > 12 Then End If X > 12 Then End End If If Response = 7 And arrQuiz(i, 2) = "0" Then MsgBox("Correct") Counter = Counter + 1 End If Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") If x > 200 Then MsgBox("Maybe it's time to diet?") Else MsgBox("Have another cookie!") End If End End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") If X > 200 Then MsgBox("Maybe it's time to diet") ElseIf X > 100 Then MsgBox("Have another cookie!") ElseIf X > 60 Then MsgBox("Please eat something!") End If End End Sub End Class If X > 60 Then ElseIf X > 100 Then ElseIf X > 200 Then If X > 59 And < 100 Then ElseIf X > 99 And < 200 Then Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") If X > 200 Then MsgBox("Maybe it's time to diet") ElseIf X > 100 Then MsgBox("Have another cookie!") ElseIf X > 60 Then MsgBox("Please eat something!") Else MsgBox("Your weight suggests that you’re under the age of 12.") End If End End Sub Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") Select Case X Case Is > 200 MsgBox("Maybe it's time to diet") Case Is > 100 MsgBox("Have another cookie!") Case Is > 60 MsgBox("Please eat something!") End Select End End Sub End Class Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") Select Case X Case 200 To 1000 MsgBox("Maybe it's time to diet") Case 100 To 200 MsgBox("Have another cookie!") Case 0 To 60 MsgBox("Please eat something!") End Select End End Sub End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim X X = Input box("What's your weight?") Select Case X Case Is > 200 MsgBox("Maybe it's time to diet?") Case Else MsgBox("Have another cookie!") End Select End End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Name = "Ashley" Select Case Name Case "Bob" MsgBox("Hi Bob!") Case "Nancy" MsgBox("Hi Nancy!") Case "Ashley" MsgBox("Hi Ashley!") Case Else MsgBox("I don't recognize that name.") End Select End End Sub Option Strict Off Option Explicit On Public Class Form1 Dim arrQuiz(3, 4) Dim QuestionNumber Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load arrQuiz(0, 0) = "Manet" arrQuiz(0, 1) = "Monet" arrQuiz(0, 2) = "Van Gogh" arrQuiz(0, 3) = "Who famously painted water lilies?" arrQuiz(0, 4) = "2" arrQuiz(1, 0) = "A painting by Van Gogh" arrQuiz(1, 1) = "A well-known planetarium in New York" arrQuiz(1, 2) = "A cereal" arrQuiz(1, 3) = "What is Starry Night?" arrQuiz(1, 4) = "1" arrQuiz(2, 0) = "love" arrQuiz(2, 1) = "war" arrQuiz(2, 2) = "Both of the above" arrQuiz(2, 3) = "What is the subject of Picasso's Guernica?" arrQuiz(2, 4) = "2" arrQuiz(3, 0) = "Sophisticated use of perspective." arrQuiz(3, 1) = "Complete lack of color." arrQuiz(3, 2) = "No use of perspective at all." arrQuiz(3, 3) = "Name one important quality of Egyptian art." arrQuiz(3, 4) = "3" 'display first question RadioButton1.Text = arrQuiz(0, 0) RadioButton2.Text = arrQuiz(0, 1) RadioButton3.Text = arrQuiz(0, 2) Label1.Text = arrQuiz(0, 3) End Sub Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click ' The Submit button was clicked ' Check their answer, report the result, update the counter Static Counter 'Holds the number of correct answers 'find out which radio button is clicked If RadioButton1.Checked = True And arrQuiz(QuestionNumber, 4) = "1" Then Label1.Text = Label1.Text & " CORRECT!" Counter = Counter + 1 ElseIf RadioButton2.Checked = True And arrQuiz(QuestionNumber, 4) = "2" Then Label1.Text = Label1.Text & " CORRECT!" Counter = Counter + 1 ElseIf RadioButton3.Checked = True And arrQuiz(QuestionNumber, 4) = "3" Then Label1.Text = Label1.Text & " CORRECT!" Counter = Counter + 1 Else Label1.Text = Label1.Text & " INCORRECT." End If Me.Text = Counter & " correct answers so far." ' raise the question number variable, redraw the question/answers or exit if finished QuestionNumber = QuestionNumber + 1 If QuestionNumber > UBound(arrQuiz) Then 'we're finished with the quiz MsgBox("You got " & Counter & " correct out of " & UBound(arrQuiz) + 1 & " questions.") End End If End Sub Private Sub btnNextQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextQuestion.Click 'the Next Question button was clicked 'redraw the form: RadioButton1.Checked = False RadioButton2.Checked = False RadioButton3.Checked = False RadioButton1.Text = arrQuiz(QuestionNumber, 0) RadioButton2.Text = arrQuiz(QuestionNumber, 1) RadioButton3.Text = arrQuiz(QuestionNumber, 2) Label1.Text = arrQuiz(QuestionNumber, 3) End Sub End Class ____________________________________________________________________________ Chapter 9 Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click End Sub End Class Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim strFilePath = "C:\temp\MyDiary.txt" SaveText(strFilePath) End End Sub End Class Dim path path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) path = path & "\MyDiary.txt" MsgBox(path) Dim path path = Application.UserAppDataPath path = path & "\MyDiary.txt" MsgBox(path) Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim strFilePath = "C:\temp\MyDiary.txt" SaveText(strFilePath) End End Sub Sub SaveText(ByVal path) Dim SWriter As New StreamWriter(path, False) 'false = don't append to an existing file SWriter.Write(TextBox1.Text) SWriter.Close() End Sub End Class Function LoadText(ByVal path As String) If Not File.Exists(path) Then MsgBox("The diary file was not found in " & path) End If Dim SReader As New StreamReader(path), strDiary strDiary = SReader.ReadToEnd() SReader.Close() Return strDiary End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim FilePath = "C:\temp\MyDiary.txt" TextBox1.Text = LoadText(FilePath) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim FilePath = "C:\temp\MyDiary.txt" TextBox1.Text = LoadText(FilePath) End Sub If Not File.Exists(path) Then MsgBox("The diary file was not found in " & path) End If Function LoadText(ByVal path As String) If Not File.Exists(path) Then MsgBox("The diary file was not found in " & path) End If Dim SReader As New StreamReader(path), strDiary strDiary = SReader.ReadToEnd() SReader.Close() Return strDiary End Function Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim strFilePath = "C:\temp\MyDiary.txt" SaveText(strFilePath) End End Sub Sub SaveText(ByVal path) Dim SWriter As New StreamWriter(path, False) 'false = don't append to an existing file SWriter.Write(TextBox1.Text) SWriter.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim FilePath = "C:\temp\MyDiary.txt" TextBox1.Text = LoadText(FilePath) End Sub Function LoadText(ByVal path As String) If Not File.Exists(path) Then MsgBox("The diary file was not found in " & path) End If Dim SReader As New StreamReader(path), strDiary strDiary = SReader.ReadToEnd() SReader.Close() Return strDiary End Function End Class ____________________________________________________________________________ Chapter 10 Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Dim arrlistDiary As New ArrayList 'an array to store the pages of the diary Dim IndexPointer ' current element's index number Now type in this code that loads the diary from the hard drive: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim FilePath = "C:\temp\arrDiary.txt" If File.Exists(FilePath) Then ' they're not using the diary program for the first time ' so load the array from the hard drive. arrlistDiary = LoadArray(FilePath) Else 'no file exists yet, so create an empty ArrayList "page" element for them to start with arrlistDiary.Add("") End If TextBox1.Text = arrlistDiary(arrlistDiary.Count - 1) IndexPointer = arrlistDiary.Count - 1 'set the array pointer (index number) to the currently displayed array element. End Sub End Class Function LoadArray(ByVal path As String) Dim ArrReader As New StreamReader(path), tempArrayList As New ArrayList, s Do s = ArrReader.ReadLine tempArrayList.Add(s) Loop Until ArrReader.EndOfStream ArrReader.Close() Return tempArrayList End Function Sub SaveArray(ByVal path, ByVal arrayList) Dim Arrwriter As New StreamWriter(path, False) 'false = don't append to an existing file For Each s In arrayList Arrwriter.WriteLine(s) Next Arrwriter.Close() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'first store the current page in the ArrayList, in case the user modified it: arrlistDiary(IndexPointer) = TextBox1.Text Dim FilePath = "C:\temp\arrDiary.txt" SaveArray(FilePath, arrlistDiary) End End Sub Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click If IndexPointer = 0 Then Exit Sub 'avoid going down out of bounds to a non-existent element(-1) ' save the current page by replacing it in the diary (in case the user has modified it) arrlistDiary(IndexPointer) = TextBox1.Text IndexPointer -= 1 'decrement pointer TextBox1.Text = arrlistDiary(IndexPointer) 'display the page that's down one in the array End Sub Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click ' save the current page by replacing it in the diary (in case the user modified it) arrlistDiary(IndexPointer) = TextBox1.Text IndexPointer += 1 'increment the pointer If IndexPointer = arrlistDiary.Count Then 'they want a new, blank page to write on because there is no higher page 'create new, empty ArrayList element arrlistDiary.Add(vbNullString) TextBox1.Text = arrlistDiary(IndexPointer) Else 'this page already exists TextBox1.Text = arrlistDiary(IndexPointer) 'display the page that's up one in the array End If End Sub Function Encrypt(ByVal s) Dim tempString = vbNullString Dim n For i = 1 To Len(s) n = Asc(Mid(s, i)) n = n + 1 tempString = tempString & Chr(n) Next Return tempString End Function Function Decrypt(ByVal s) Dim tempString = vbNullString Dim n For i = 1 To Len(s) n = Asc(Mid(s, i)) n = n - 1 tempString = tempString & Chr(n) Next Return tempString End Function Function LoadArray(ByVal path As String) Dim ArrReader As New StreamReader(path), tempArrayList As New ArrayList, s Do s = ArrReader.ReadLine s = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream ArrReader.Close() Return tempArrayList End Function Sub SaveArray(ByVal path, ByVal arrayList) Dim Arrwriter As New StreamWriter(path, False) 'false = don't append to an existing file For Each s In arrayList s = Encrypt(s) Arrwriter.WriteLine(s) Next Arrwriter.Close() End Sub Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Dim arrlistDiary As New ArrayList 'an array to store the pages of the diary Dim IndexPointer ' current element's index number Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim FilePath = "C:\temp\arrDiary.txt" If File.Exists(FilePath) Then ' they're not using the diary program for the first time ' so load the array from the hard drive. arrlistDiary = LoadArray(FilePath) Else 'no file exists yet, so create an empty ArrayList "page" element for them to start with arrlistDiary.Add("") End If TextBox1.Text = arrlistDiary(arrlistDiary.Count - 1) IndexPointer = arrlistDiary.Count - 1 'set the array pointer (index number) to the currently displayed array element. End Sub Function LoadArray(ByVal path As String) Dim ArrReader As New StreamReader(path), tempArrayList As New ArrayList, s Do s = ArrReader.ReadLine s = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream ArrReader.Close() Return tempArrayList End Function Sub SaveArray(ByVal path, ByVal arrayList) Dim Arrwriter As New StreamWriter(path, False) 'false = don't append to an existing file For Each s In arrayList s = Encrypt(s) Arrwriter.WriteLine(s) Next Arrwriter.Close() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'first store the current page in the ArrayList, in case the user modified it: arrlistDiary(IndexPointer) = TextBox1.Text Dim FilePath = "C:\temp\arrDiary.txt" SaveArray(FilePath, arrlistDiary) End End Sub Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click If IndexPointer = 0 Then Exit Sub 'avoid going down out of bounds to a non-existent element(-1) ' save the current page by replacing it in the diary (in case the user has modified it) arrlistDiary(IndexPointer) = TextBox1.Text IndexPointer -= 1 'decrement pointer TextBox1.Text = arrlistDiary(IndexPointer) 'display the page that's down one in the array End Sub Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click ' save the current page by replacing it in the diary (in case the user modified it) arrlistDiary(IndexPointer) = TextBox1.Text IndexPointer += 1 'increment the pointer If IndexPointer = arrlistDiary.Count Then 'they want a new, blank page to write on because there is no higher page 'create new, empty ArrayList element arrlistDiary.Add(vbNullString) TextBox1.Text = arrlistDiary(IndexPointer) Else 'this page already exists TextBox1.Text = arrlistDiary(IndexPointer) 'display the page that's up one in the array End If End Sub Function Encrypt(ByVal s) Dim tempString = vbNullString Dim n For i = 1 To Len(s) n = Asc(Mid(s, i)) n = n + 1 tempString = tempString & Chr(n) Next Return tempString End Function Function Decrypt(ByVal s) Dim tempString = vbNullString Dim n For i = 1 To Len(s) n = Asc(Mid(s, i)) n = n - 1 tempString = tempString & Chr(n) Next Return tempString End Function End Class ____________________________________________________________________________ Chapter 11 Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bitM Dim fName = "c:\temp\zoron.bmp" 'attempt to load the image: bitM = Image.FromFile(fName) Me.BackgroundImage = bitM End Sub End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bitM Dim fName = "c:\temp\zoron.bmp" Try 'attempt to load the image: bitM = Image.FromFile(fName) Catch MsgBox("The file named " & fName & " could not be found. Please save a file by that name to that location. Then re-run this program.") End End Try Me.BackgroundImage = bitM End Sub Option Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bitM Dim fName = "c:\temp\zoron.bmp" Try 'attempt to load the image: bitM = Image.FromFile(fName) Catch When Err.Number = 7 MsgBox("There's an out of memory error problem:", , _ Err.Description) End Catch When Err.Number = 53 MsgBox("The file named " & fName & " could not be found. Please save a file by that name to that location. Then re-run this program.") End Catch MsgBox("Problem loading file", , Err.Description) End End Try Me.BackgroundImage = bitM End Sub End Class Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i = 1 To 100 Debug.Print(i & ". " & ErrorToString(i)) Next i End End Sub End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bitM Dim fName = "c:\temp\zoron.bmp" Try 'attempt to load the image: bitM = Image.FromFile(fName) Catch ex As System.IO.IOException MsgBox(ex.ToString) End TryOption Strict Off Option Explicit On Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bitM Dim fName = "c:\temp\zoron.bmp" Try 'attempt to load the image: bitM = Image.FromFile(fName) Catch MemoryErr As OutOfMemoryException MsgBox("There's an out of memory error problem. The system error message is: " & MemoryErr.Message) End Catch FileErr As FileNotFoundException MsgBox("The file named " & fName & " could not be found. " & _ "Please save a file by that name to that location. " & _ "Then re-run this program. The system error message is: " & FileErr.Message) End Catch UnspecifiedErr As Exception MsgBox("Problem loading file. The system error message is: " & UnspecifiedErr.Message) End End Try Me.BackgroundImage = bitM End Sub End Class Catch MemoryErr As OutOfMemoryException MsgBox("There's an out of memory error problem. The system error message is: " & MemoryErr.Message) Do s = ArrReader.ReadLine 's = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream For Each s In arrayList 's = Encrypt(s) Arrwriter.WriteLine(s) Next Do s = ArrReader.ReadLine 's = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream Do s = ArrReader.ReadLine 's = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream Public Class Password Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "raffles" Then Me.Hide() Form1.Show() Else End End If End Sub End Class Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'first store the current page in the arraylist, in case the user modified it: arrlistDiary(IndexPointer) = TextBox1.Text Dim FilePath = "C:\temp\arrDiary.txt" SaveArray(FilePath, arrlistDiary) End End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 'first store the current page in the arraylist, in case the user modified it: arrlistDiary(IndexPointer) = TextBox1.Text Dim FilePath = "C:\temp\arrDiary.txt" SaveArray(FilePath, arrlistDiary) End End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing btnExit_Click(Me, e) End Sub ____________________________________________________________________________ Chapter 12 No code ____________________________________________________________________________ Chapter 13 EndTime = Int32.Parse( Interaction.Input box("Please type in the delay in minutes...", "", "", 0, 0)); Endtime = InputBox("Please type in the delay in minutes...") Option Strict Off Option Explicit On Sub TimeIt() Dim CurrentTime As Integer Dim EndTime EndTime = Input box("Please type in the delay in minutes...") EndTime = EndTime + Minute(Now) Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop MsgBox("Time's Up!") End End Sub public void TimeIt() { int CurrentTime = 0; int EndTime = 0; EndTime = Int32.Parse( Interaction.Input box("Please type in the delay in minutes...", "", "", 0, 0)); EndTime = EndTime + DateTime.Now.Minute; while (CurrentTime < EndTime) { CurrentTime = DateTime.Now.Minute; } Message box.Show("Time's Up!"); Application.Exit(); } CurrentTime: Dim CurrentTime As Integer int CurrentTime = 0; Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop while (CurrentTime < EndTime) { CurrentTime = DateTime.Now.Minute; } MsgBox("Time's Up!") End Message box.Show("Time's Up!"); Application.Exit(); Message box.Show ("Time's Up!"); Application.Exit(); MsgBox _ ("Time's up") if (Counter >= TotalSeconds) { Timer1.Stop(); Message box.Show ("Time's up."); Application.Exit(); For, To, Step, and Next: For i = 3 To 30 Step 3 MsgBox (i) Next for (i = 3; i <= 30; i += 3) { Message box.Show (i); } Dim Guests(4) As String string[] Guests = new string[5]; ____________________________________________________________________________ Appendix A Do s = ArrReader.ReadLine s = Decrypt(s) tempArrayList.Add(s) Loop Until ArrReader.EndOfStream Loop Until ArrReader.EndOfStream Do While CurrentTime < EndTime CurrentTime = Minute(Now) Loop Do CurrentTime = Minute(Now) Loop While CurrentTime < EndTime Do Until CurrentTime = EndTime CurrentTime = Minute(Now) Loop If X = 51 Then End End If X = UsersWeight If X > 200 Then MsgBox("It's time to diet.") Else MsgBox("Have another cookie!") End If Dim X As String X = InputBox("What's your weight?") If X > 200 Then MsgBox("Maybe it's time to diet") ElseIf X > 100 Then MsgBox("Have another cookie!") ElseIf X > 60 Then MsgBox("Please eat something!") End If Dim X As String X = InputBox("What's your weight?") Select Case CInt(X) Case Is > 200 MsgBox("Maybe it's time to diet") Case Is > 100 MsgBox("Have another cookie!") Case Is > 60 MsgBox("Please eat something!") End Select Dim X As String X = InputBox("What's your weight?") Select Case CInt(X) Case Is > 200 MsgBox("Maybe it's time to diet?") Case Else MsgBox("Have another cookie!") End Select Case 200 To 1000 MsgBox("Maybe it's time to diet") Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Markdown() End Sub Sub Markdown() End Sub Option Strict Off Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strName, strResult strName = "Macon County" strResult = RaiseCase(strName) MsgBox(strResult) End End Sub Function RaiseCase(ByVal s) s = UCase(s) Return s End Function End Class Dim Num1 = 68 Dim Num2 = 77 If Num1 < Num2 Then MsgBox("Num1 is less") End If If 5 + 2 = 4 Or 6 + 6 = 12 Then MsgBox ("One of them is true.") Dim Str1 As String = "Mark " Dim Str2 As String = "Twain" MsgBox(Str1 & Str2) Dim strAddress Dim strAddress = "Norton" Dim strAddress, strTown, strState Static Toggle Dim arrGuests(12) As String For i = 0 To 12 Next Dim arrGuests(12) As String For i = 0 To UBound(arrGuests) Next s = InputBox("How old are you") Dim arrGuests(12) As String MsgBox(UBound(arrGuests)) ____________________________________________________________________________ Appendix B no code ____________________________________________________________________________ Appendix C no code ____________________________________________________________________________ Appendix D no code ____________________________________________________________________________ Glossary Dim MyArray(112) As String MyArray(42) = "Carole" Dim s As String = "nancy" Dim s1 As String = "Nancy" If s = s1 Then MsgBox("They are identical") Dim Toggle As Boolean Dim Num1 As Integer = 14 Dim Num2 As Integer = 7 If Num1 > Num2 Then Dim StateTax = 1.07 Dim s As String = Chr(13) Dim s As String = vbCr For i = 1 To 14 Button1.Hide() Label1.BackColor = _ Color.AntiqueWhite Label1.BackColor = Color.AntiqueWhite If x = 0 Then MsgBox("We're finished.") : End Static Toggle