Home > Blockchain >  How can I load in Windows Forms? (C#)
How can I load in Windows Forms? (C#)

Time:10-26

I have 2 Windows Forms. In the second form I have a few checkedListBoxex and my problem is that when I'm trying to get those checks and save it for the next time, it's not saving them, maybe I did a small mistake somewhere. I think it should be problem with load.

My code:

public partial class Form2 : Form
    {
        readonly Form1 form1;
        StringCollection collectionOfTags = new StringCollection();
       

        public Form2(Form1 owner)
        {
            form1 = owner;
            InitializeComponent();
            InitializeSecondForm();
            
        }

        private void InitializeSecondForm()
        {
            this.Height = Properties.Settings.Default.SecondFormHeight;
            this.Width = Properties.Settings.Default.SecondFormWidth;
            this.Location = Properties.Settings.Default.SecondFormLocation;
            this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;

            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.DICOMTagSettings = this.collectionOfTags;

            Properties.Settings.Default.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                foreach (string s in checkedListBox1.CheckedItems)
                    Properties.Settings.Default.DICOMTagSettings.Add(s);
             collectionOfTags = Properties.Settings.Default.DICOMTagSettings;

foreach (string s in checkedListBox2.CheckedItems)
                    Properties.Settings.Default.DICOMTagSettings.Add(s);
             collectionOfTags = Properties.Settings.Default.DICOMTagSettings;

foreach (string s in checkedListBox3.CheckedItems)
                    Properties.Settings.Default.DICOMTagSettings.Add(s);
             collectionOfTags = Properties.Settings.Default.DICOMTagSettings;
             this.Close();
            }

This is how it looks in settings.

enter image description here

This one I added just by typing it.

enter image description here

When I'm debugging, I can see that I have some items there, but it's not saving them there.

enter image description here

CodePudding user response:

The checked items are being saved to Properties.Settings.Default.DICOMTagSettings, then, they're being loaded to collectionOfTags, but you're not actually using collectionOfTags to update the checked items.

The collectionOfTags variable is redundant actually (unless you need it for something else). You could just access the string collection directly from settings. Change your code to something like the following.

To save the checked items:

Properties.Settings.Default.DICOMTagSettings.Clear();
foreach (string s in checkedListBox1.CheckedItems)
{
    Properties.Settings.Default.DICOMTagSettings.Add(s);
}

Or you could replace the foreach loop above with this one liner:

Properties.Settings.Default.DICOMTagSettings
    .AddRange(checkedListBox1.CheckedItems.Cast<string>().ToArray());

To update the checked items when the form loads:

foreach (string s in Properties.Settings.Default.DICOMTagSettings)
{
    int index = checkedListBox1.Items.IndexOf(s);
    if (index != -1) checkedListBox1.SetItemChecked(index , true);
}

CodePudding user response:

I created a method:

private void LoadSettings() 
{
  for (int i = 0; i < checkedListBox1.Items.Count; i  )
        {
            var d = checkedListBox1.Items[i];
            if (collectionOfTags.Contains(d.ToString()))
            {
                int index = checkedListBox1.Items.IndexOf(d);
                if (index != -1)
                    checkedListBox1.SetItemChecked(index, true);
            }
        }
}

Put this method in Constructor:

public Form2(Form1 owner)
        {
            form1 = owner;
            InitializeComponent();
            InitializeSecondForm();

            LoadSettings();

        }

Now it's working, thank you for the help @41686d6564.

  • Related