enter image description here

There is a listbox attachment filter. This code is used to save the settings:

Properties.Settings.Default["Path"] = tbPath.Text; Properties.Settings.Default["SiteName"] = cbxSite.Text; Properties.Settings.Default["MailAdress"] = tbMail.Text; Properties.Settings.Default["MailPassword"] = tbMailPassword.Text; Properties.Settings.Default["Latency"] = Convert.ToInt32(cbxLatency.Text); Properties.Settings.Default["DeleteMails"] = cbDelete.Checked; 

The question is, I use System.Collections.Specialized.StringCollection in the settings for the filter, but I can not figure out how to save them.

To upload data from settings I use this code:

  tbPath.Text = Properties.Settings.Default["Path"].ToString(); cbxSite.Text = Properties.Settings.Default["SiteName"].ToString(); tbMail.Text = Properties.Settings.Default["MailAdress"].ToString(); tbMailPassword.Text = Properties.Settings.Default["MailPassword"].ToString(); cbxLatency.Text = Properties.Settings.Default["Latency"].ToString(); cbDelete.Checked = Convert.ToBoolean(Properties.Settings.Default["DeleteMails"]); 

And again, I can not figure out how to unload the data type StringCollection.

2 answers 2

The problem was solved by a couple of foreach cycles.

I leave the code to those who can come in handy:

Filling data:

 foreach (string item in lbFilter.Items) { Properties.Settings.Default.Filter.Add(item); } Properties.Settings.Default.Save(); 

Data upload:

 foreach(string s in Properties.Settings.Default.Filter) { if (!lbFilter.Items.Contains(s)) { lbFilter.Items.Add(s); } } Properties.Settings.Default.Filter.Clear(); 

    For example, take this set of controls. sample program interface

    Create for them the corresponding variables. Pay attention to the types of variables. variables for saving settings

    Then you can read and save the settings

     public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { _textBox.Text = Properties.Settings.Default.textBox; _checkBox.Checked = Properties.Settings.Default.checkBox; _comboBox.SelectedIndex = Properties.Settings.Default.comboBox; List<bool> values = Properties.Settings.Default.checkedList.OfType<string>() .Select(s => s.Equals("1") ? true : false) .ToList(); for (int i = 0; i < _checkedListBox.Items.Count; i++) { _checkedListBox.SetItemChecked(i, values[i]); } } private void _buttonSave_Click(object sender, EventArgs e) { Properties.Settings.Default.textBox = _textBox.Text; Properties.Settings.Default.checkBox = _checkBox.Checked; Properties.Settings.Default.comboBox = _comboBox.SelectedIndex; for (int i = 0; i < _checkedListBox.Items.Count; i++) { Properties.Settings.Default.checkedList[i] = _checkedListBox.GetItemChecked(i) ? "1" : "0"; } //сохраняем настройки Properties.Settings.Default.Save(); } }