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.
Re: VB.NET | Saving ListBox with Additional information in My.settings
"it doesn't work" ... "it doesn't work" ....
"doc it hurts when I do this" "well then don't do that" ...
Well if that doesn't work.. don't do that.
Sounds like the problem is you have some eels in your hovercraft. Fortunately, this is such a common problem, that there is a guide available... you can find a link to it in my signature block "How to get rid of those pesky eels" ...
-tg
Re: VB.NET | Saving ListBox with Additional information in My.settings
OT - a class that represents a single student should not be named Students. Don't pluralise names arbitrarily. That class should be named Student and a variable that referred to an array or collection of Student objects could be named students, much like your setting. Also, a ListBox that stores student data probably ought to be named studentsListBox. Descriptive names are always better than the default names, except for maybe in quick and dirty examples and demos.
Re: VB.NET | Saving ListBox with Additional information in My.settings
Your setting is type StringCollection. It's not storing instances of your class but rather Strings. You need to convert your objects to reasonable String representations in order to save, which would mean a String containing the name AND age values. You then need to convert those Strings back to Students objects on load.