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

  • Thread starter Thread starter floyare
  • Start date Start date
F

floyare

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


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:

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.

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.

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

Continue reading...
 

Similar threads

Back
Top