Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSave and restore ListBox items when a program stops and starts in Visual Basic .NET
DescriptionThis example shows how to save and restore ListBox items when a program stops and starts in Visual Basic .NET.
KeywordsListBox, save, restore, VB.NET
CategoriesVB.NET, Controls, Software Engineering
 
This program allows the user to add and remove items from a ListBox. When the user clicks the Add button, the following code uses an InputBox to get a new item and then adds it to the list.
 
' Add a new item.
Private Sub btnAdd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAdd.Click
    Dim new_item As String = InputBox("New value", "New " & _
        "Value")
    If new_item.Length = 0 Then Exit Sub

    lstFoods.Items.Add(new_item)
End Sub
 
When the user clicks Remove, the program executes the following code. The code loops through the items in the ListBox and, if an item is selected, removes it from the list.
 
' Remove the selected item(s).
Private Sub btnRemove_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnRemove.Click
    ' Count backwards to avoid numbering issues when
    ' removing items.
    For i As Integer = lstFoods.Items.Count - 1 To 0 Step -1
        If lstFoods.GetSelected(i) Then
            lstFoods.Items.RemoveAt(i)
        End If
    Next i
End Sub
 
The form's Load event handler uses the following code to reload the list's saved items from the Registry. It loops through items named Item1, Item2, and so forth adding each item it finds in the Registry to the list. When it fails to find one of the named items, it exits its loop.
 
Private Const APP_NAME As String = "SaveRestoreListBox"
Private Const SECTION_NAME As String = "Items"

' Load saved items from the Registry.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    lstFoods.Items.Clear()

    Dim i As Integer = 0
    Do
        Dim new_value As String = GetSetting(APP_NAME, _
            SECTION_NAME, "Item" & i.ToString())
        If new_value.Length = 0 Then Exit Do

        lstFoods.Items.Add(new_value)
        i += 1
    Loop
End Sub
 
The form's Closing event handler uses the following code to save the list's current items. It starts by deleting any previously saved items. It protects itself with a Try Catch block in case there are no saved items (this happens the first time you run the program).

Next the program loops through the list's items saving them in the Registry with the names Item1, Item2, and so forth.

 
' Save the current items.
Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
    As System.ComponentModel.CancelEventArgs) Handles _
    MyBase.Closing
    ' Delete existing items.
    ' Catch errors in case the section doesn't exist.
    Try
        DeleteSetting(APP_NAME, SECTION_NAME)
    Catch ex As Exception
    End Try

    ' Save the items.
    For i As Integer = 0 To lstFoods.Items.Count - 1
        SaveSetting(APP_NAME, SECTION_NAME, "Item" & _
            i.ToString(), lstFoods.Items(i).ToString())
    Next i
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated