I am creating a recipe program that will allow the user to save recipes in an external file, retrieve them and work out a new value for ingrediants depending on how many people they want this time. However within my code errors keep appearing and I cannot work out why they do. Here is my code, please help if you can;
Imports System.IO
Public Class Form1
Dim Unit, nameofrecipe, nameofingrediant, oldrecipe, recipewanted, filecontents As String
Dim quantity, originalamountofpeople, newpeople As Integer
Dim filename As String
Dim filewriter As IO.StreamWriter
Dim filereader As IO.StreamReader
Dim txt_ As String
Dim counter As Integer
Private Sub btnenternameandpeople_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenternameandpeople.Click
nameofrecipe = (txtnameofrecipe.Text)
originalamountofpeople = (txtoriginalpeople.Text)
filename = (CurDir() & txtnameofrecipe.Text & ".txt")
lstdisplayenteredrecipe.Items.Add(nameofrecipe & " " & "for" & " " & originalamountofpeople)
lstdisplayenteredrecipe.SelectedIndex = lstdisplayenteredrecipe.SelectedIndex + 1
End Sub
Private Sub btnenteringrediant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenteringrediant.Click
Unit = txtunit.Text
quantity = Val(txtquantity.Text)
nameofingrediant = txtnameofingrediant.Text
lstdisplayenteredrecipe.Items.Add(nameofingrediant & "," & quantity & "," & Unit)
lstdisplayenteredrecipe.SelectedIndex = lstdisplayenteredrecipe.SelectedIndex + 1
txtunit.Clear()
txtquantity.Clear()
txtnameofingrediant.Clear()
End Sub
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim i As Integer
filewriter = New StreamWriter(filename, True) ' the 'true' means it appends the text rather than deletes the old part
For i = 0 To lstdisplayenteredrecipe.Items.Count - 1
filewriter.WriteLine(lstdisplayenteredrecipe.Items.Item(i))
Next
filewriter.Close()
txtoriginalpeople.Clear()
txtnameofingrediant.Clear()
txtnameofrecipe.Clear()
txtunit.Clear()
txtquantity.Clear()
End Sub
Private Sub btnnewrecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewrecipe.Click
Dim wholeingrediant As String
Dim ingrediant As String
Dim originalquantity As Integer
Dim newquantity As Integer
Dim oldpeople As Integer = 10
Dim unit As String
Dim counter As Integer = 0
filename = (CurDir() & txtfindoldrecipe.Text & ".txt")
If My.Computer.FileSystem.FileExists(filename) Then
filereader = New StreamReader(filename)
Do
wholeingrediant = filereader.ReadLine
Dim line As String() = Split(wholeingrediant, ",")
counter = counter + 1
For Each Str As String In line
counter = counter + 1
If counter = 1 Then
ingrediant = Str
lstnewrecipe.Items.Add(ingrediant)
End If
If counter = 2 Then
originalquantity = Val(Str)
newquantity = originalquantity / oldpeople * Val(txtnewpeople.Text)
lstnewrecipe.Items.Add(newquantity)
End If
If counter = 3 Then
unit = Str
lstnewrecipe.Items.Add(unit)
End If
Next
Loop Until line Is Nothing
Else : MsgBox("This recipe has not been entered before, please enter original quantities on the left of the screen", MsgBoxStyle.Information)
End If
End Sub
Private Sub btnfinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfinish.Click
filereader.Close()
' so the user does not have to come out of the program to enter another recipe
Me.Close()
End Sub
End Class