View Single Post
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: can someone help me with saving my data in a file program?

 
0
  #5
Dec 8th, 2008
I think its a good idea to keep data entry and data viewing as separate forms.

Here's a bit fixing to data entry form (saveButton_Click). Replace control names (TextBox1 <- modelTextBox etc.) and add missing boxes.
  1. ' Declare writer
  2. Private autoStreamWriter As StreamWriter
  3. ' Set file name as a constant value
  4. Const FileName As String = "C:\Vehicles.txt"
  5.  
  6. ' I use ValidateForm as a separate "form validator"
  7. Private Function ValidateForm() As Boolean
  8. '
  9. If TextBox1.Text.Length = 0 Then
  10. MessageBox.Show("Model is empty. All fields must be entered before the information can be saved.")
  11. TextBox1.Focus() ' Move focus to missing field
  12. Return False
  13. End If
  14. ' Next Textboxes here
  15. ' Validation is Ok
  16. Return True
  17.  
  18. End Function
  19.  
  20. ' Save button
  21. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  22. '
  23. ' Check fields
  24. If Not ValidateForm() Then
  25. Exit Sub
  26. End If
  27.  
  28. Try
  29. ' Create writer
  30. autoStreamWriter = New StreamWriter(FileName, True)
  31. autoStreamWriter.WriteLine(TextBox1.Text)
  32. autoStreamWriter.WriteLine(TextBox2.Text)
  33. ' Add missing WriteLine... here
  34. autoStreamWriter.Close()
  35. TextBox1.Clear()
  36. TextBox2.Clear()
  37. ' Add missing Clear... here
  38. ' Set focus to Model box
  39. TextBox1.Focus()
  40. Catch ex As Exception
  41. ' Handle exception
  42. ' Can't open file etc. can occur, so trap exceptions
  43. End Try
  44.  
  45. End Sub
And here's the code to read data. You may create an Auto class object but I used structure and array to hold data. This code goes in your "View data"-form.
  1. ' Data reader
  2. Private autoStreamReader As StreamReader
  3. Const FileName As String = "C:\Vehicles.txt"
  4.  
  5. ' Structure to hold information for a one Auto object
  6. Private Structure Auto
  7. Dim Model As String
  8. Dim Manufacturer As String
  9. Dim Year As String
  10. Dim VIN As Integer ' I did assume VIN is an integer number
  11. End Structure
  12.  
  13. ' Array of Auto objects
  14. Private Autos() As Auto
  15. ' Number of items
  16. Private AutoCount As Integer
  17.  
  18. ' This "Read Data"-button
  19. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  20. ' Add similar Try...Catch...End Try here too (file can be missing etc.)
  21. Dim i As Integer
  22.  
  23. ' Set count to -1 (no data)
  24. AutoCount = -1
  25. autoStreamReader = New StreamReader(FileName)
  26. ' Read the whole file
  27. Do Until autoStreamReader.EndOfStream
  28. ' Increase counter
  29. AutoCount += 1
  30. ' Redim array
  31. ReDim Preserve Autos(AutoCount)
  32. ' Read one Auto object
  33. Autos(AutoCount).Model = autoStreamReader.ReadLine
  34. Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine
  35. Autos(AutoCount).Year = autoStreamReader.ReadLine
  36. Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine)
  37. Loop
  38. ' Fill a combo box with VINs
  39. For i = 0 To AutoCount
  40. ComboBox1.Items.Add(Autos(i).VIN)
  41. Next i
  42. '
  43. End Sub
  44.  
  45. ' A modified SelectedIndexChanged to select an Auto object and show its data
  46. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  47. ' Get selected Auto object
  48. Dim ThisAuto As Auto
  49.  
  50. Try
  51. ThisAuto = Autos(ComboBox1.SelectedIndex)
  52. ' Place information in the labels
  53. Label1.Text = ThisAuto.Model
  54. ' and so on...
  55. Catch ex As Exception
  56. ' Handle exception
  57. End Try
  58.  
  59. End Sub
Hope this gets you started
Reply With Quote