Saving Listbox items to settings c#

Hemanth B 886 Reputation points
2021-07-26T03:08:35.553+00:00

How do I save the items of a listbox to Properties.Settings? Also how should I load those items on FormLoad? I've searched the web but I couldn't any helpful result. In settings, should I set the type to string? Please help!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,937 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-07-26T06:28:07.147+00:00

    Hi HemanthB-9452,
    First, you need to add a "listboxItems" settingļ¼š
    Name - listboxItems, Type - System.Collections.ArrayList, Scope - User
    Note that maybe you need to follow the steps to find System.Collections.ArrayList type:
    Expand the type drop-down box->Browser->Input System.Collections.ArrayList in Selected type->ok
    117790-726.png
    Then refer to the following code:

    private void Form1_Load(object sender, EventArgs e)  
    {  
        // add all items from listbox1 to settings  
        var newList = new ArrayList();  
        foreach (object item in listBox1.Items)  
        {  
            newList.Add(item);  
        }  
      
        Properties.Settings.Default.listboxItems = newList;  
        Properties.Settings.Default.Save();  
        //you could load the list back like so:  
        listBox2.Items.AddRange(Properties.Settings.Default.listboxItems.ToArray());  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.