mnbcxz 0 Newbie Poster

Hi I am having a problem coding a save/change button on my form which I want to be able to edit a record in a text box then save the changes made, it will save it in a sequential dat file. At the moment it will just overwrite the entry below it, I think it may have to do with global variables, also trying to get previous button to go as it goes forward first before going back to the previous record any help would be much appreciated.

Imports System.IO

Public Class Form1

    Public input As FileStream
    Public output As FileStream
    Public fileReader As StreamReader
    Public fileWriter As StreamWriter
    Public fileName As String = "members.dat"
    Public inputfield() As String
    Public fuelsarray() = IO.File.ReadAllLines(fileName)
    Public I As Integer
    Public d As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim fileChooser As New OpenFileDialog()
        Dim result As DialogResult = fileChooser.ShowDialog()
        Dim fileName As String

        If result = Windows.Forms.DialogResult.Cancel Then
            Return
        End If

        fileName = fileChooser.FileName

        If fileName = "" Or fileName Is Nothing Then
            MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            input = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            fileReader = New StreamReader(input)

        End If

    End Sub

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


        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        If I > fuelsarray.GetUpperBound(0) Then
            MsgBox("You are at the end of the file can't go beyond!", MsgBoxStyle.Information)
            I = fuelsarray.GetUpperBound(0)
            Exit Sub

        End If

        Dim inputRecord As String

        inputRecord = fuelsarray(I)

        inputfield = inputRecord.Split(","c)

        TextBox1.Text = inputfield(0)
        TextBox2.Text = inputfield(1)
        TextBox3.Text = inputfield(2)
        TextBox4.Text = inputfield(3)
        TextBox5.Text = inputfield(4)
        TextBox6.Text = inputfield(5)
        TextBox7.Text = inputfield(6)
        TextBox8.Text = inputfield(7)
        d = I
        I = I + 1

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        fileReader.Close()
        input.Close()

        output = New FileStream(fileName, FileMode.Append, FileAccess.Write)
        fileWriter = New StreamWriter(output)
        fileWriter.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox7.Text & "," & TextBox8.Text)

        fileWriter.Close()
        output.Close()

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        MsgBox("Record added!", MsgBoxStyle.Information)

        TextBox1.Focus()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

     
        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        Dim objStreamWriter As StreamWriter

        objStreamWriter = New StreamWriter(fileName, False)
        fuelsarray(I) = TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox7.Text & "," & TextBox8.Text

        For t As Integer = 0 To fuelsarray.GetUpperBound(0)
            objStreamWriter.WriteLine(fuelsarray(t))
        Next



        objStreamWriter.Close()

    End Sub

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


        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        Dim objStreamWriter As StreamWriter

        objStreamWriter = New StreamWriter(fileName, False)
        For t As Integer = d To fuelsarray.GetUpperBound(0) - 1
            fuelsarray(t) = fuelsarray(t + 1)
        Next


        For t As Integer = 0 To fuelsarray.GetUpperBound(0) - 1
            objStreamWriter.WriteLine(fuelsarray(t))
        Next

        objStreamWriter.Close()


    End Sub

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


        Dim fuelsarray() = IO.File.ReadAllLines(fileName)
        fileReader.Close()
        input.Close()

        Dim inputRecord As String

        If I < 0 Then
            MsgBox("You are at the first record can't go beyond the first record!", MsgBoxStyle.Information)
            I = 0
            Exit Sub

        End If

        inputRecord = fuelsarray(I)

        inputfield = inputRecord.Split(","c)

        TextBox1.Text = inputfield(0)
        TextBox2.Text = inputfield(1)
        TextBox3.Text = inputfield(2)
        TextBox4.Text = inputfield(3)
        TextBox5.Text = inputfield(4)
        TextBox6.Text = inputfield(5)
        TextBox7.Text = inputfield(6)
        TextBox8.Text = inputfield(7)
        d = I
        I = I - 1

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click


        Dim fileName As String = "members.dat"
        Dim filereader As New IO.StreamReader(fileName)

        ListBox1.Items.AddRange(Split(filereader.ReadToEnd, _
            vbCrLf))
        ListBox1.SelectedIndex = 0
        filereader.Close()
       



    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Application.Exit()


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class