Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
1,465 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:Murray Brown
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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:Murray Brown
ID: 37823665
Great answer! Thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

877 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