[B] Need help editing sequential file[B]

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: May 2008
Posts: 5
Reputation: mnbcxz is an unknown quantity at this point 
Solved Threads: 0
mnbcxz mnbcxz is offline Offline
Newbie Poster

[B] Need help editing sequential file[B]

 
0
  #1
May 27th, 2008
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.


  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.  
  5. Public input As FileStream
  6. Public output As FileStream
  7. Public fileReader As StreamReader
  8. Public fileWriter As StreamWriter
  9. Public fileName As String = "members.dat"
  10. Public inputfield() As String
  11. Public fuelsarray() = IO.File.ReadAllLines(fileName)
  12. Public I As Integer
  13. Public d As Integer = 0
  14.  
  15. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  16.  
  17.  
  18. Dim fileChooser As New OpenFileDialog()
  19. Dim result As DialogResult = fileChooser.ShowDialog()
  20. Dim fileName As String
  21.  
  22. If result = Windows.Forms.DialogResult.Cancel Then
  23. Return
  24. End If
  25.  
  26. fileName = fileChooser.FileName
  27.  
  28. If fileName = "" Or fileName Is Nothing Then
  29. MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  30. Else
  31. input = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
  32. fileReader = New StreamReader(input)
  33.  
  34. End If
  35.  
  36. End Sub
  37.  
  38. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  39.  
  40.  
  41. fileReader.Close()
  42. input.Close()
  43.  
  44. Dim fuelsarray() = IO.File.ReadAllLines(fileName)
  45.  
  46. If I > fuelsarray.GetUpperBound(0) Then
  47. MsgBox("You are at the end of the file can't go beyond!", MsgBoxStyle.Information)
  48. I = fuelsarray.GetUpperBound(0)
  49. Exit Sub
  50.  
  51. End If
  52.  
  53. Dim inputRecord As String
  54.  
  55. inputRecord = fuelsarray(I)
  56.  
  57. inputfield = inputRecord.Split(","c)
  58.  
  59. TextBox1.Text = inputfield(0)
  60. TextBox2.Text = inputfield(1)
  61. TextBox3.Text = inputfield(2)
  62. TextBox4.Text = inputfield(3)
  63. TextBox5.Text = inputfield(4)
  64. TextBox6.Text = inputfield(5)
  65. TextBox7.Text = inputfield(6)
  66. TextBox8.Text = inputfield(7)
  67. d = I
  68. I = I + 1
  69.  
  70. End Sub
  71.  
  72. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  73.  
  74.  
  75. fileReader.Close()
  76. input.Close()
  77.  
  78. output = New FileStream(fileName, FileMode.Append, FileAccess.Write)
  79. fileWriter = New StreamWriter(output)
  80. fileWriter.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox7.Text & "," & TextBox8.Text)
  81.  
  82. fileWriter.Close()
  83. output.Close()
  84.  
  85. TextBox1.Text = ""
  86. TextBox2.Text = ""
  87. TextBox3.Text = ""
  88. TextBox4.Text = ""
  89. TextBox5.Text = ""
  90. TextBox6.Text = ""
  91. TextBox7.Text = ""
  92. TextBox8.Text = ""
  93. MsgBox("Record added!", MsgBoxStyle.Information)
  94.  
  95. TextBox1.Focus()
  96.  
  97. End Sub
  98.  
  99. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  100.  
  101.  
  102. fileReader.Close()
  103. input.Close()
  104.  
  105. Dim fuelsarray() = IO.File.ReadAllLines(fileName)
  106.  
  107. Dim objStreamWriter As StreamWriter
  108.  
  109. objStreamWriter = New StreamWriter(fileName, False)
  110. fuelsarray(I) = TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox7.Text & "," & TextBox8.Text
  111.  
  112. For t As Integer = 0 To fuelsarray.GetUpperBound(0)
  113. objStreamWriter.WriteLine(fuelsarray(t))
  114. Next
  115.  
  116.  
  117.  
  118. objStreamWriter.Close()
  119.  
  120. End Sub
  121.  
  122. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  123.  
  124.  
  125. fileReader.Close()
  126. input.Close()
  127.  
  128. Dim fuelsarray() = IO.File.ReadAllLines(fileName)
  129.  
  130. Dim objStreamWriter As StreamWriter
  131.  
  132. objStreamWriter = New StreamWriter(fileName, False)
  133. For t As Integer = d To fuelsarray.GetUpperBound(0) - 1
  134. fuelsarray(t) = fuelsarray(t + 1)
  135. Next
  136.  
  137.  
  138. For t As Integer = 0 To fuelsarray.GetUpperBound(0) - 1
  139. objStreamWriter.WriteLine(fuelsarray(t))
  140. Next
  141.  
  142. objStreamWriter.Close()
  143.  
  144.  
  145. End Sub
  146.  
  147. Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  148.  
  149.  
  150. Dim fuelsarray() = IO.File.ReadAllLines(fileName)
  151. fileReader.Close()
  152. input.Close()
  153.  
  154. Dim inputRecord As String
  155.  
  156. If I < 0 Then
  157. MsgBox("You are at the first record can't go beyond the first record!", MsgBoxStyle.Information)
  158. I = 0
  159. Exit Sub
  160.  
  161. End If
  162.  
  163. inputRecord = fuelsarray(I)
  164.  
  165. inputfield = inputRecord.Split(","c)
  166.  
  167. TextBox1.Text = inputfield(0)
  168. TextBox2.Text = inputfield(1)
  169. TextBox3.Text = inputfield(2)
  170. TextBox4.Text = inputfield(3)
  171. TextBox5.Text = inputfield(4)
  172. TextBox6.Text = inputfield(5)
  173. TextBox7.Text = inputfield(6)
  174. TextBox8.Text = inputfield(7)
  175. d = I
  176. I = I - 1
  177.  
  178. End Sub
  179.  
  180. Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
  181.  
  182.  
  183. Dim fileName As String = "members.dat"
  184. Dim filereader As New IO.StreamReader(fileName)
  185.  
  186. ListBox1.Items.AddRange(Split(filereader.ReadToEnd, _
  187. vbCrLf))
  188. ListBox1.SelectedIndex = 0
  189. filereader.Close()
  190.  
  191.  
  192.  
  193.  
  194. End Sub
  195.  
  196. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
  197. Application.Exit()
  198.  
  199.  
  200. End Sub
  201.  
  202. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  203.  
  204. End Sub
  205. End Class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC