dcsimg
Results 1 to 4 of 4

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

  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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,803

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,755

    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.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,755

    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.

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
  •  



Featured


Click Here to Expand Forum to Full Width