【问题标题】:How can I load in Windows Forms? (C#)如何在 Windows 窗体中加载? (C#)
【发布时间】:2021-10-25 10:23:42
【问题描述】:

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

这是它在设置中的样子。

这个是我输入才添加的。

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

【问题讨论】:

  • 您是否在项目的默认设置文件中添加了所述属性。请参阅:stackoverflow.com/questions/1873658/…
  • 1) 您确定在关闭表单之前单击了button1?因为那是您保存检查项目的唯一地方。 2) 当表单重新打开时,您是否在某处使用collectionOfTags 来更新选中的项目?因为您没有在代码中的任何地方显示它。
  • @Zeeshanef 我在 Settings.settings DICOMTagSettings 中输入了 System.Collections.Specialized.StringCollection。
  • @41686d6564 1) 我确定这个按钮。 2) collectionOfTags = Properties.Settings.Default.DICOMTagSettings - 我正在放入这个集合,然后我试图保存它。喜欢:this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;和 Properties.Settings.Default.DICOMTagSettings = this.collectionOfTags;。我不确定这是不是正确的方法。
  • “这就是它在设置中的样子” 这就是它应该的样子。保存的项目不会出现在那里(以防万一)。要确定项目是否已保存,您需要在调试时在运行时检查DICOMTagSettings 集合。

标签: c# winforms


【解决方案1】:

选中的项目被保存到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);
}

【讨论】:

  • 我尝试了您的解决方案,但出现了一些错误。 1) 参数 1:无法从 'System.Collections.Generic.IEnumerable' 转换为 'string[]'。它是 Properties.Settings.Default.DICOMTagSettings.AddRange(checkedListBox1.CheckedItems.Cast()); 2)当我尝试执行此操作时,foreach(Properties.Settings.Default.DICOMTagSettings 中的字符串 s){ checkedListBox1.SetItemChecked(checkedListBox1.Items.IndexOf(s), true);我得到错误。错误:System.ArgumentOutOfRangeException:'InvalidArgument='-1' 的值对'index' 无效。
  • @Sanktos 1) 我的错。它应该转换为数组。固定的。 2) 您需要向我们展示您是如何填写 CheckedListBox 项目的。你确定它们都是字符串吗?请edit 提问并提供minimal reproducible example
  • 是的,第一个正在工作。我会添加更多。
  • @Sanktos 您编辑了问题,但没有显示如何填充(即添加项目)CheckedListBox。请这样做。也就是说,您似乎一直在用来自多个 CheckedListBox 的项目填充 StringCollection,而不仅仅是一个。这可能是 ArgumentOutOfRangeException 的原因。我对上面的答案(最后一个代码块)所做的编辑可能解决了这个问题,但在我看到你如何将项目添加到列表框之前我不能确定。
  • 现在我没有任何错误,但是当我关闭第二个表单时,它没有保存任何内容。它应该在checkedListBox中保存检查,但仍然没有。
【解决方案2】:

我创建了一个方法:

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

将此方法放入构造函数中:

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

            LoadSettings();

        }

现在可以了,感谢@41686d6564 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多