Save and restore ListBox items

Started by nandagopal, May 20, 2009, 07:08 PM

Previous topic - Next topic

nandagopal

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



dhoni

the save an restore program has easily given on listbox item
this should  use in this item for vb.net
the program easily get from this website
.net program has easily use

jessicaamirr

This must be very easy but I am stuck. I have a listBox with X Items. Each Item has a text description (Which appears in the listBox) and its value(numerical). I want to be able to get the value property of an item, using the index number of the item. :D

----------------------------------------------------------------------------------------------------------------------------------------
Dot net Training in Chennai | Cloud Computing Training in Chennai | Dot net Training in Chennai | Cloud Computing Training in Chennai

acemary

#3
Is there a way to set a "Settings.Default.mylistbox" variable so that it will store the whole collection of a ListBox or ComboBox?

acemary

#4
Is there a way to set a "Settings.Default.mylistbox" variable so that it will store the whole collection of a ListBox or ComboBox?

adleyami

#5
Is there a way to set a "Settings.Default.mylistbox" variable so that it will store the whole collection of a ListBox or ComboBox?

Suresh Kumar

Thanks for sharing this informatics content and keep updating us. This content is really helpful to me in developing the Cloud Computing Skills.
Cloud Computing Training in Chennai | Cloud Computing Training | Cloud Computing Courses

hopesuresh

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.

Python Training in Chennai
AngularJS Training in Chennai
Best AngularJS Training in Chennai

WicultyLS

"Master DevOps with Wiculty Learning Solutions: Comprehensive Certification and Exceptional Placement Support"

Embark on a transformative journey with Wiculty Learning Solutions' comprehensive DevOps certification program. Crafted to provide in-depth knowledge and practical expertise, our curriculum covers essential DevOps tools and methodologies, including continuous integration, continuous deployment, infrastructure as code, and monitoring and logging. Through hands-on projects and real-world scenarios, we ensure that our students are fully equipped to meet the high standards of the industry.
Wiculty Learning Solutions goes beyond exemplary instruction by offering outstanding placement support. Our dedicated placement team is committed to helping graduates secure job opportunities by providing personalized resume-building assistance, intensive interview preparation, and direct connections with top tech companies. This unique blend of rigorous education and robust placement support guarantees that our graduates are thoroughly prepared to excel in their DevOps careers.
Learn more about this innovative program and take the first step towards a successful DevOps career with Wiculty DevOps Certification Training Course.
https://wiculty.com/devops-certification-training-course-btm-layout/



Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview