簡體   English   中英

如何將列表框項目保存到項目設置

[英]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();
}

我想在列表框中添加文件夾並保存在設置中,這些項目在 FormLoad 中加載 /?? 可以在 Form Load 中加載項目嗎? 提前致謝 !

假設您的listboxitems是一個StringCollection對象,該對象已添加到用戶范圍中的項目設置中(應用程序范圍中的設置不能直接更新),您可以使用BindingSource處理您的字符串集合。
此類可以將其內部列表綁定到幾乎任何集合,並且對其內部列表的任何更改都反映在綁定到它的集合中。
這當然包括從集合中添加和刪除項目。

注意:在這里,您的listboxitems設置被重命名為ListBoxItems (使用適當的大小寫)
listBox5listBox5中發生了someListBox (➨ 建議為您的 Controls 賦予有意義的名稱)。

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;
}

現在,要將新項目添加到 ListBox,同時添加到 StringCollection 對象(您的listboxitems設置),只需向 BindingSource 添加一個新字符串:它會自動更新自己的源列表。 然后您可以立即保存設置(例如,如果應用程序突然終止,則防止數據丟失)。 或者在任何其他時間執行此操作。

// 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();

要從集合中刪除項目,當數據顯示在 ListBox 中時,您可能需要考慮 ListBox SelectionMode
如果它不是SelectionMode.One ,則必須處理多個選擇: SelectedIndices屬性返回所選項目的索引。
按降序對索引進行排序(刪除項目而不修改索引序列)並使用BindingSource.RemoveAt()方法刪除每個選定的項目。

如果只有一個選定的 Item 並且使用 ListBox 執行選擇,則可以使用BindingSource.RemoveCurrent()方法。
如果需要刪除通過其他方式(例如,TextBox)指定的字符串,則需要使用BindingSource.Remove()方法刪除字符串本身。 請注意, is 將僅刪除匹配的第一個字符串。

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();

我想在列表框中添加多個文件

 //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(); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM