如何加载 Windows 表单? (C#)
How can I load in Windows Forms? (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();
}
这是它在设置中的样子。
这是我打字添加的。
当我调试时,我可以看到我在那里有一些项目,但它并没有将它们保存在那里。
检查的项目被保存到 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);
}
我创建了一个方法:
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 的帮助。
我有 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();
}
这是它在设置中的样子。
这是我打字添加的。
当我调试时,我可以看到我在那里有一些项目,但它并没有将它们保存在那里。
检查的项目被保存到 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);
}
我创建了一个方法:
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 的帮助。