| | |
can someone help me with saving my data in a file program?
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
I am writing a project that stores vehicle information such as the model,manuf., year, etc.
I have to create a second project that loads the data from the file into memory and loads a drop down combo box with the VIN numbers. When the number is selected from the box, it is supposed to display the appropriate information in labels. Im having problems storing my information and creating this project because the book doesn't explain it very well. Can anyone point me in the right direction?
I have to create a second project that loads the data from the file into memory and loads a drop down combo box with the VIN numbers. When the number is selected from the box, it is supposed to display the appropriate information in labels. Im having problems storing my information and creating this project because the book doesn't explain it very well. Can anyone point me in the right direction?
You didn't mention, how the information is stored. But if you have numbers in your combo box and you can fetch the information with that number, you need to trap SelectedIndexChanged for the combo box:
Hope this gets you started. If you still have a problem, you need to tell more details about how you store your information (database, a file...)
VB.NET Syntax (Toggle Plain Text)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ' Get selected VIN number Dim ThisVINNumber As Integer Try ThisVINNumber = CInt(ComboBox1.Items(ComboBox1.SelectedIndex)) ' Fetch information for this VIN and place it in the labels Catch ex As Exception ' Handle exception End Try End Sub
Teme64 @ Windows Developer Blog
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
Since i'm storing my information in a file, do i need to make a .txt file first and then write the program? Also, Since i'm creating two projects under the same thing, how would i go about making my forms? My first idea was to have a form for the user to enter in the data and then have another form that loads that information from the .txt file. I have in mind what I want to do but I just dont know the right way to execute it.
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
Here is the code that i have so far, but my .txt file won't execute:
Imports System.IO
Public Class createForm
'Declare module level variables.
Dim autoStreamWriter As New StreamWriter("Vehicles.txt")
Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
If modelTextBox.Text <> "" And manufacturerTextBox.Text <> "" And yearTextBox.Text <> "" And vinNumTextBox.Text <> "" Then
autoStreamWriter = New StreamWriter("Vehicles.txt", True)
autoStreamWriter.WriteLine(modelTextBox.Text)
autoStreamWriter.WriteLine(manufacturerTextBox.Text)
autoStreamWriter.WriteLine(yearTextBox.Text)
autoStreamWriter.WriteLine(vinNumTextBox.Text)
autoStreamWriter.Close()
Else
MessageBox.Show("All fields must be entered before the information can be saved.")
End If
With Me
autoStreamWriter.WriteLine(modelTextBox.Text)
autoStreamWriter.WriteLine(manufacturerTextBox.Text)
autoStreamWriter.WriteLine(yearTextBox.Text)
autoStreamWriter.WriteLine(vinNumTextBox.Text)
With .modelTextBox
.Clear()
.Focus()
End With
.manufacturerTextBox.Clear()
.yearTextBox.Clear()
.vinNumTextBox.Clear()
End With
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Close the file and the form.
autoStreamWriter.Close()
Me.Close()
End Sub
End Class 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.
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.
Hope this gets you started
Here's a bit fixing to data entry form (saveButton_Click). Replace control names (TextBox1 <- modelTextBox etc.) and add missing boxes.
VB.NET Syntax (Toggle Plain Text)
' Declare writer Private autoStreamWriter As StreamWriter ' Set file name as a constant value Const FileName As String = "C:\Vehicles.txt" ' I use ValidateForm as a separate "form validator" Private Function ValidateForm() As Boolean ' If TextBox1.Text.Length = 0 Then MessageBox.Show("Model is empty. All fields must be entered before the information can be saved.") TextBox1.Focus() ' Move focus to missing field Return False End If ' Next Textboxes here ' Validation is Ok Return True End Function ' Save button Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' ' Check fields If Not ValidateForm() Then Exit Sub End If Try ' Create writer autoStreamWriter = New StreamWriter(FileName, True) autoStreamWriter.WriteLine(TextBox1.Text) autoStreamWriter.WriteLine(TextBox2.Text) ' Add missing WriteLine... here autoStreamWriter.Close() TextBox1.Clear() TextBox2.Clear() ' Add missing Clear... here ' Set focus to Model box TextBox1.Focus() Catch ex As Exception ' Handle exception ' Can't open file etc. can occur, so trap exceptions End Try End Sub
VB.NET Syntax (Toggle Plain Text)
' Data reader Private autoStreamReader As StreamReader Const FileName As String = "C:\Vehicles.txt" ' Structure to hold information for a one Auto object Private Structure Auto Dim Model As String Dim Manufacturer As String Dim Year As String Dim VIN As Integer ' I did assume VIN is an integer number End Structure ' Array of Auto objects Private Autos() As Auto ' Number of items Private AutoCount As Integer ' This "Read Data"-button Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click ' Add similar Try...Catch...End Try here too (file can be missing etc.) Dim i As Integer ' Set count to -1 (no data) AutoCount = -1 autoStreamReader = New StreamReader(FileName) ' Read the whole file Do Until autoStreamReader.EndOfStream ' Increase counter AutoCount += 1 ' Redim array ReDim Preserve Autos(AutoCount) ' Read one Auto object Autos(AutoCount).Model = autoStreamReader.ReadLine Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine Autos(AutoCount).Year = autoStreamReader.ReadLine Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine) Loop ' Fill a combo box with VINs For i = 0 To AutoCount ComboBox1.Items.Add(Autos(i).VIN) Next i ' End Sub ' A modified SelectedIndexChanged to select an Auto object and show its data Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ' Get selected Auto object Dim ThisAuto As Auto Try ThisAuto = Autos(ComboBox1.SelectedIndex) ' Place information in the labels Label1.Text = ThisAuto.Model ' and so on... Catch ex As Exception ' Handle exception End Try End Sub
Teme64 @ Windows Developer Blog
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
Since i'm using two forms, how would i get the streamwriter to appear first? My plan was to have the createForm (streamwriter) to have a menu strip so that when the user selects like Open Summary or something, the other form will display for the user to go through the records using the vin number combo box
You mean the data entry form is the "main" form? You can set menu item to disabled (Enabled = False).
Or you don't even have to do that. If the user selects "Open Summary" (and the file is empty) the user gets an empty summary form.
Here's a fixed "Read Data"-button, which checks first that the file exists. It also closes the StreamReader, a minor bug in my previous posting:
Or you don't even have to do that. If the user selects "Open Summary" (and the file is empty) the user gets an empty summary form.
Here's a fixed "Read Data"-button, which checks first that the file exists. It also closes the StreamReader, a minor bug in my previous posting:
VB.NET Syntax (Toggle Plain Text)
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click ' Dim i As Integer AutoCount = -1 If Not My.Computer.FileSystem.FileExists(FileName) Then Exit Sub End If Try autoStreamReader = New StreamReader(FileName) Do Until autoStreamReader.EndOfStream AutoCount += 1 ReDim Preserve Autos(AutoCount) Autos(AutoCount).Model = autoStreamReader.ReadLine Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine Autos(AutoCount).Year = autoStreamReader.ReadLine Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine) Loop Catch ex As Exception ' Handle exception Finally autoStreamReader.Close() End Try ' For i = 0 To AutoCount ComboBox1.Items.Add(Autos(i).VIN) Next i ' End Sub
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Saving pickled info and reloading info (Python)
- saving database file into a text file using vb6 (Visual Basic 4 / 5 / 6)
- save file to database (Java)
- Saving to a file (C)
- Perl/CGI (Saving Data) Part III (Computer Science)
- Perl/CGI (Reading Data) Part II (Computer Science)
- fstream and saving to a:\ (C++)
- reading and saving data to textfiles... (Visual Basic 4 / 5 / 6)
- Problems of looping in saving to a text file (C)
Other Threads in the VB.NET Forum
- Previous Thread: deleting the records of the MS access table using VB.NET
- Next Thread: The ConnectionString property has not been initialized
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account arithmetic array basic bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker dissertation dissertations dissertationtopic dropdownlist eclipse excel fade file-dialog filter ftp generatetags google gridview hardcopy images input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters pdf picturebox picturebox1 port position print printing problem project read remove save searchbox searchvb.net select serial server settings shutdown soap survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio2008 web winforms wpf year





