Results 1 to 4 of 4

Thread: VB.NET | Saving ListBox with Additional information in My.settings

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    8

    VB.NET | Saving ListBox with Additional information in My.settings

    Hi, I recently started learning listboxes on VB.net and wanted to create something like a student list with additional information:

    Code:
        
    Private Class Students
            Private m_Name As String
            Private m_Age As String
    
            Public Sub New(ByVal new_Name As String, ByVal _
            new_Age As String
            )
                m_Name = new_Name
                m_Age = new_Age
            End Sub
    
            Public Overrides Function ToString() As String
                Return m_Name
            End Function
    
            Public Function Age() As String
                Return m_Age
            End Function
        End Class
    So, students add to the listbox as follows:

    Code:
    ListBox1.Items.Add(New Students(StudentName.Text, StudentAge.Text)) 'StudentName and  StudentAge are textboxes.
    but I also wanted to save this information so that it is automatically entered when the program is restarted. I tried to do first an event that saves every item in the ListBox but using the function above it doesn't work.

    Code:
        
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    
            For Each item In ListBox1.Items
                My.Settings.Students.Add(item)
                My.Settings.Save()
            Next
    
        End Sub
    Then I would like to load this information, but just like the event with saving information when closing the program it does not work.

    Code:
        
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If My.Settings.Students Is Nothing Then
                My.Settings.Students = New Specialized.StringCollection
            Else
                For Each item In My.Settings.Students
                    ListBox1.Items.Add(item)
                Next
            End If
    
        End Sub
    I would like to have these the information in listbox after load not in .txt files or something like that.
    Last edited by mmmx19; Nov 26th, 2019 at 11:49 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width