简体   繁体   中英

How to save Listbox Items to Project settings

private void Form1_Load(object sender, EventArgs e)
{
    var newList = Properties.Settings.Default.listboxitems;
    foreach (object item in listBox5.Items)
    {
        newList.Add(item);
        listBox5.Items.Add(item);
    }
}

private void button57_Click(object sender, EventArgs e)
{
   string s = Path.GetFileName(folderName);

    listBox5.Items.Add(s);
    var newList = new ArrayList();

    foreach (object item in listBox5.Items)
    {
        newList.Add(item);
    }

    Properties.Settings.Default.listboxitems = newList;
    Properties.Settings.Default.Save();
}

I want to Add Folder in Listbox and Save in Setting, These item is load in FormLoad /?? Is that possible to Load Items in Form Load ? Thanks in Advance !

Assuming your listboxitems is a StringCollection object added to the Project's Settings in the User scope (settings in the Application scope cannot be updated direcly), you can use a BindingSource handle your collection of strings.
This class can bind its internal list to almost any collection and any change to its internal list is reflected in the collection bound to it.
This of course include adding and removing items from the collection.

Note: here, your listboxitems setting is renamed ListBoxItems (using proper case)
listBox5 is changed in someListBox (➨ suggests to give your Controls meaningful names).

using System.Linq;

BindingSource listBoxSource = null;

public Form1()
{
    InitializeComponent();
    // [...]
    // Initialize the BindingSource using the ListBoxItems setting as source
    listBoxSource = new BindingSource(Properties.Settings.Default.ListBoxItems, "");
    // Set the BindingSource as the source of data of a ListBox
    someListBox.DataSource = listBoxSource;
}

Now, to add a new Item to the ListBox and, at the same time, to the StringCollection object (your listboxitems Setting), just add a new string to the BindingSource: it will automatically update its own source list. You can then save the Settings immediately after (eg, to prevent data loss if the application terminates abruptly). Or do this at any other time.

// One item
listBoxSource.Add("New Item");
// Or multiple items
foreach (string item in [Some collection]) {
    listBoxSource.Add(item);
}

// Save the Settings if required
Properties.Settings.Default.Save();

To remove Items from the collection, when the data is presented in a ListBox, you may need to consider the ListBox SelectionMode .
If it's not SelectionMode.One , you'll have to handle multiple selections: the indices of the selected Items is returned by the SelectedIndices property.
Order the indices in descending order (to remove Items without modifying the indexes sequence) and use the BindingSource.RemoveAt() method to remove each selected item.

If there's just one selected Item and the selection is performed using the ListBox, you can use the BindingSource.RemoveCurrent() method.
If you need to remove a string specified in by other means (eg, a TextBox), you need to remove the string itself, using the BindingSource.Remove() method. Note that is will remove only the first string that matches.

if (someListBox.SelectedIndices.Count == 0) return;
if (someListBox.SelectedIndices.Count > 1) {
    foreach  (int idx in someListBox.SelectedIndices.
        OfType<int>().OrderByDescending(id => id).ToArray()) {
        listBoxSource.RemoveAt(idx);
    }
}
else {
    listBoxSource.RemoveCurrent();
}
// Save the Settings if required
Properties.Settings.Default.Save();

I want to Add Multiple File in List box

 //Add Files in Listbox private void button57_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "Please select the directory that includes images"; if (dialog.ShowDialog() == DialogResult.OK) { string folderName = dialog.SelectedPath; string s = Path.GetFileName(folderName); listboxsource.Add(s); Properties.Settings.Default.Save(); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM