Solved

VB.net Save new ListBox items so that they are loaded back in next time the form is open

Posted on 2012-04-09
2
1,182 Views
Last Modified: 2012-04-09
Hi


In VB.net 2010 I have a ListBox that the user adds to or deletes items from. I want to be able to save what is in the ListBox, which may contain up to 100 entries. What is the best way to do this? Should I perhaps make one long string out of it and save this as a Setting, or should I write this to a text file. I am always weary of the latter because some of the issue of not being able to write to certain drives and as far as the former goes, I'm not sure how long a string one can save in a single setting.

Any suggestions would be very helpful.

Thanks
0
Comment
Question by:murbro
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37823602
Saving as one long string in Settings would work just fine.  You'd need to pick your delimiter carefully though.  Will the comma "," or pipe "|" ever appear in the entries of the ListBox?

Here's a simple example using the pipe:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim values As String = My.Settings.ListBoxValues
        If values <> "" Then
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(values.Split("|"))
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim values As New List(Of String)
        For Each item As String In ListBox1.Items
            values.Add(item)
        Next
        My.Settings.ListBoxValues = String.Join("|", values.ToArray)
        My.Settings.Save()
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 37823665
Great answer! Thanks very much
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question