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,109 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now