首页 > 解决方案 > 如何在 Windows 窗体中加载?(C#)

问题描述

我有 2 个 Windows 窗体。在第二种形式中,我有一些checkedListBoxex,我的问题是,当我尝试获取这些检查并将其保存以备下次使用时,它并没有保存它们,也许我在某个地方犯了一个小错误。我认为这应该是负载问题。

我的代码:

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

这是它在设置中的外观。

在此处输入图像描述

这是我通过键入添加的。

在此处输入图像描述

当我调试时,我可以看到那里有一些项目,但它没有将它们保存在那里。

在此处输入图像描述

标签: c#winforms

解决方案


选中的项目被保存到Properties.Settings.Default.DICOMTagSettings,然后,它们被加载到collectionOfTags,但您实际上并没有用来 collectionOfTags更新选中的项目。

collectionOfTags变量实际上是多余的(除非您需要它来做其他事情)。您可以直接从设置中访问字符串集合。将您的代码更改为如下所示。

要保存选中的项目:

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

或者你可以foreach用这个衬里替换上面的循环:

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

要在表单加载时更新选中的项目:

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

推荐阅读