简体   繁体   中英

How to save many object (with the same class) to txt file and binding those object with listbox VB.NET

I try to program a simple project to save data to txt file, read it and use binding data to show it. My project like this.

在此处输入图片说明

When I add ID Person to "Add ID" textbox (Textbox which near Button "Add ID"). It will add ID to Listbox and "ID Name" textbox. With this IDName, I insert FirstName and LastName for first person and Save Person's Name. Then, I Add new ID in "Add ID" textbox and fill First,last name and save it again

在此处输入图片说明 在此处输入图片说明

I refer this page http://vbnetsample.blogspot.de/2007/12/serialize-deserialize-class-to-file.html?m=1 to save and read data to txt file. It's run ok. But my problem is that when I save Person 2 with ID 2, Person 1 is overwrited. I think I can save Person to List Person. But it will make difficult when I want to update any Person's data. I don't know whether Is there any way to save and update each person. And by the way How can I show data by binding data in listbox.

Here is my code

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class Form1
    Public pPerson As New Person

    'Serialize and Save Data to txt file
    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        pPerson.IDName = IDNameTextBox.Text
        pPerson.FirstName = FirstNameTextBox.Text
        pPerson.LastName = LastNameTextBox.Text
        Dim fs As FileStream = New FileStream("C:\Users\Bruce\Desktop\test.txt", FileMode.OpenOrCreate)
        Dim bf As New BinaryFormatter()
        bf.Serialize(fs, pPerson)
        fs.Close()
    End Sub

    'Deserialize and Read Data from txt file 
    Private Sub ReadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
        If FileIO.FileSystem.FileExists("C:\Users\Bruce\Desktop\test.txt") Then
            Dim fsRead As New FileStream("C:\Users\Bruce\Desktop\test.txt", FileMode.Open)
            Dim bf As New BinaryFormatter()
            Dim objTest As Object = bf.Deserialize(fsRead)
            fsRead.Close()
            IDNameTextBox.Text = objTest.IDName
            FirstNameTextBox.Text = objTest.FirstName
            LastNameTextBox.Text = objTest.LastName
        End If
    End Sub

    Private Sub AddIDButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddIDButton.Click
        ListBox1.Items.Insert(0, AddIDTextBox.Text)
        IDNameTextBox.Text = AddIDTextBox.Text
    End Sub
End Class

'Create Class Person
<System.Serializable()>
Public Class Person
    Private m_sIDName As String
    Private m_sFirstName As String
    Private m_sLastName As String

    Public Sub New()
    End Sub
    Public Property IDName() As String
        Get
            Return Me.m_sIDName
        End Get
        Set(ByVal value As String)
            Me.m_sIDName = value
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return Me.m_sFirstName
        End Get
        Set(ByVal value As String)
            Me.m_sFirstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return Me.m_sLastName
        End Get
        Set(ByVal value As String)
            Me.m_sLastName = value
        End Set
    End Property

End Class

To me it seems like the issue here is

Dim fs As FileStream = New FileStream("C:\Users\Bruce\Desktop\test.txt", FileMode.OpenOrCreate)

you have to change it to this:

If not File.Exists("C:\Users\Bruce\Desktop\test.txt") Then
File.create("C:\Users\Bruce\Desktop\test.txt")
End If
Dim fs As FileStream = New FileStream("C:\Users\Bruce\Desktop\test.txt", FileMode.Append)

The whole problem was that your file was simply open (or created if it was not there before) and being written from the first line.By using FileMode.Append your File will be opened and anything new will be tried to be written at the end of the file.

Let me know if this worked :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM