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,352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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