| | |
[B] Need help editing sequential file[B]
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
VB.NET Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Renaming files in C++ (C++)
Other Threads in the VB.NET Forum
- Previous Thread: Status Bar Marquee...Need Help!
- Next Thread: Datareader...i need help urgently...
| Thread Tools | Search this Thread |
"crystal .net .net2005 .net2008 2008 access add application array assignment basic beginner box browser button buttons click code combo convert cpu cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationthesis dissertationtopic dosconsolevb.net editvb.net exists fade filter firewall forms html image images input isnumericfuntioncall listview math mobile module mssqlbackend mysql navigate number opacity open panel picturebox picturebox2 port print printing printpreview record regex reports" reuse right-to-left savedialog serial settings shutdown socket sqldatbase sqlserver storedprocedure string temp temperature textbox timer timespan transparency txttoxmlconverter useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vbnet vista visual visualbasic.net visualstudio.net web winforms wpf wrapingcode xml year





