Bloodhail
- Quote
- Posted: Wed, 08 Feb 2012 11:27:51 +0000
Thank you for the original help. Now I'm running into a different problem. I am using the streamwriter object to copy the contents of an array into a txt file. The problem is that it doesnt write the user's input, it writes "WindowsApplication1.Form1 + Person" Person is a structure that I am using. If you need any more info to help me out please ask.
here's the code for the program:
here's the code for the program:
Option Explicit On
Option Infer Off
Option Strict On
Public Class Form1
Structure Person
Public strName As String
Public strHeight As String
Public strweight As String
End Structure
Dim Persons(49) As Person
Dim incoming(49) As String
Dim inthighestrowsub As Integer = 0
Dim outfile As IO.StreamWriter
Dim infile As IO.StreamReader
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Persons(inthighestrowsub).strName = CStr(txtName.Text.PadRight(15))
Persons(inthighestrowsub).strHeight = CStr(txtHeight.Text.PadRight(15))
Persons(inthighestrowsub).strweight = CStr(txtWeight.Text)
inthighestrowsub = inthighestrowsub + 1
txtName.Text = ""
txtHeight.Text = ""
txtWeight.Text = ""
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim y As Integer = 0
If IO.File.Exists("records.txt") Then
outfile = IO.File.AppendText("records.txt")
'write array into file'
Do Until y = inthighestrowsub
outfile.WriteLine(Persons(y))
y = y + 1
Loop
outfile.Close()
Else : outfile = IO.File.CreateText("records.txt")
Do Until y = inthighestrowsub
outfile.WriteLine(Persons(y))
y = y + 1
Loop
outfile.Close()
End If
'this section goes last'
Dim x As Integer = 0
Do Until x = 49
Persons(x).strName = ""
Persons(x).strHeight = ""
Persons(x).strweight = ""
x = x + 1
Loop
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim x As Integer = 0
Dim y As Integer = 0
Dim read1 As String
If IO.File.Exists("records.txt") Then
infile = IO.File.OpenText("records.txt")
Do While infile.Peek <> -1
read1 = read1 & infile.ReadLine & ControlChars.NewLine
Loop
lblResults.Text = CStr("Name:").PadRight(15) & CStr("Height:").PadRight(15) & CStr("Weight:").PadRight(15) & ControlChars.NewLine & read1
Else
MessageBox.Show("There is no file to read. Please press save to create the file.")
End If
infile.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lblResults.Text = ""
Dim stream As New IO.StreamWriter("G:COMP9LAB2COMP9LAB2binDebugrecords.txt", False)
stream.WriteLine("")
stream.Close()
End Sub
End Class
Option Infer Off
Option Strict On
Public Class Form1
Structure Person
Public strName As String
Public strHeight As String
Public strweight As String
End Structure
Dim Persons(49) As Person
Dim incoming(49) As String
Dim inthighestrowsub As Integer = 0
Dim outfile As IO.StreamWriter
Dim infile As IO.StreamReader
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Persons(inthighestrowsub).strName = CStr(txtName.Text.PadRight(15))
Persons(inthighestrowsub).strHeight = CStr(txtHeight.Text.PadRight(15))
Persons(inthighestrowsub).strweight = CStr(txtWeight.Text)
inthighestrowsub = inthighestrowsub + 1
txtName.Text = ""
txtHeight.Text = ""
txtWeight.Text = ""
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim y As Integer = 0
If IO.File.Exists("records.txt") Then
outfile = IO.File.AppendText("records.txt")
'write array into file'
Do Until y = inthighestrowsub
outfile.WriteLine(Persons(y))
y = y + 1
Loop
outfile.Close()
Else : outfile = IO.File.CreateText("records.txt")
Do Until y = inthighestrowsub
outfile.WriteLine(Persons(y))
y = y + 1
Loop
outfile.Close()
End If
'this section goes last'
Dim x As Integer = 0
Do Until x = 49
Persons(x).strName = ""
Persons(x).strHeight = ""
Persons(x).strweight = ""
x = x + 1
Loop
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim x As Integer = 0
Dim y As Integer = 0
Dim read1 As String
If IO.File.Exists("records.txt") Then
infile = IO.File.OpenText("records.txt")
Do While infile.Peek <> -1
read1 = read1 & infile.ReadLine & ControlChars.NewLine
Loop
lblResults.Text = CStr("Name:").PadRight(15) & CStr("Height:").PadRight(15) & CStr("Weight:").PadRight(15) & ControlChars.NewLine & read1
Else
MessageBox.Show("There is no file to read. Please press save to create the file.")
End If
infile.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lblResults.Text = ""
Dim stream As New IO.StreamWriter("G:COMP9LAB2COMP9LAB2binDebugrecords.txt", False)
stream.WriteLine("")
stream.Close()
End Sub
End Class