【发布时间】: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集合。