Nov 24, 2017, 02:29 PM

News:

IT Acumens Proudly Presents "Gin Gly": Sms Backup / Contacts Backup / Sms Grouping Website Used by Members 85,000 - SMS Backed up 7,25,000 - Contacts Stored  28,850. For 5 Years !!


Save and restore ListBox items

Started by nandagopal, May 21, 2009, 12:38 AM

previous topic - next topic
Go Down

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.

Code: [Select]

' 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
Apr 05, 2016, 08:54 PM Last Edit: Jun 09, 2016, 04:29 PM by Sudhakar Reason: removed link
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
Apr 20, 2016, 10:24 PM Last Edit: Jun 09, 2016, 04:29 PM by Sudhakar Reason: removed link
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
May 20, 2016, 05:00 PM Last Edit: Jun 09, 2016, 04:29 PM by Sudhakar Reason: removed link
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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

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's 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 submit/post or alt+p preview
IT Acumens | GinGly :: SMS Backup | Acumen :: Discussion Board | AshokPillar :: Hosting | CineBuzz :: Latest Cinema News | My Kids Diary :: Capture your kids magical moment
Copyright 2005 - 2017 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 10 years - Powered by HostGator Dedicated Server