In my app, I have a few of checkListBoxes and I need to save clicks on this list. That means for the next time I don't need to click on them again and again.
Code:
public Form2()
{
InitializeComponent();
InitializeSecondForm();
}
private void InitializeSecondForm()
{
this.Height = Properties.Settings.Default.SecondFormHeight;
this.Width = Properties.Settings.Default.SecondFormWidth;
this.Location = Properties.Settings.Default.SecondFormLocation;
this.FormClosing += SecondFormClosingEventHandler;
this.StartPosition = FormStartPosition.Manual;
}
private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SecondFormHeight = this.Height;
Properties.Settings.Default.SecondFormWidth = this.Width;
Properties.Settings.Default.SecondFormLocation = this.Location;
Properties.Settings.Default.Save();
}
I tried to use this question for my answer, but it's not working: Save CheckedListBox Items to Settings
With a simple checkBox it's not a problem, but here we have a list.
Any idea how to do it?
Perhaps saving identifiers of each checked item to a json file.
In the following I did on CheckedListBox, for multiple CheckedListBox controls you need to adjust the code to use one json file with a modified structure to account for multiple CheckedListBox control or one json file per CheckedListBox.
Example, load items into the following class
Use the following class for read/write to a json file, in this case using
json.netbut will work also withsystem.text.json.The following classes provide methods to get checked items set checked items from json mentioned above.
public class CheckItem { public Product Product { get; set; } public int Index { get; set; } }
Form code would resemble the following to read checked items on form shown event and save checked item on form closing event.