This may seem like a dumb question. I have a textbox that can be used to add items to a checkedlistbox at runtime on a windows form. I'm using c#. It works perfectly fine at runtime. The item gets added and stuff, when the form is open. But, when I close and open the form again, I don't see the added item in the checkedlistbox list. Note, I don't use a datasource and don't want to. I wouldn't want to hardcode anything and would prefer to use a textbox input on the form as a variable to feed into the collections list. I couldn't figure out a way to expand my checkedlistbox options. Any assistance would be appreciated.
这似乎是一个愚蠢的问题。我有一个文本框,可用于在Windows窗体上运行时将项目添加到checkedlistbox。我正在使用c#。它在运行时完美运行。当表单打开时,项目会被添加和填充。但是,当我再次关闭并打开表单时,我没有在checkedlistbox列表中看到添加的项目。注意,我不使用数据源而不想。我不想硬编码任何东西,并且更喜欢使用表单上的文本框输入作为变量来输入集合列表。我无法找到扩展我的checkedlistbox选项的方法。任何援助将不胜感激。
3 个解决方案
#1
How are you opening the form? Is it something like:
你是如何打开表格的?是这样的:
FormName form = new FormName();
form.Show()
The only reason I can think that's happening is that you're instantiating a new form instance every time you show it, instead of reusing the same form.
我认为发生这种情况的唯一原因是,每次显示时都会实例化一个新的表单实例,而不是重复使用相同的表单。
#2
Have your Form take a ref List<string> values
as parameter. Then make this as BindingSource for the CheckedListBox.
让您的表单将ref List
Here is the code:
这是代码:
class MyForm : Form {
List<string> values;
BindingSource source;
public MyForm()
{
InitializeComponent();
}
public MyForm(ref List<string> values):this()
{
if (values == null)
values = new List<string>();
this.values = values;
checkedListBox1.DisplayMember = "Value";
checkedListBox1.ValueMember = "Value";
source = new BindingSource(this.values, null);
checkedListBox1.DataSource = source;
}
private void AddItemButton_Click(object sender, EventArgs e)
{
this.source.Add(textBox1.Text);
textBox1.Text = string.Empty;
}
}
#3
private void frmMain_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[] checkedIndicies = Properties.Settings.Default.CheckedItems.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i1 = 0; i1 < checkedIndicies.Length; i1++)
{
int idx;
if ((int.TryParse(checkedIndicies[i1], out idx)) && (checkedListBox1.Items.Count >= (idx+1)))
{
checkedListBox1.SetItemChecked(idx, true);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.MaxLength = 15;
// Change all text entered to be lowercase.
textBox1.CharacterCasing = CharacterCasing.Lower;
if (checkedListBox1.Items.Contains(textBox1.Text) == false)
{
checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);
textBox1.Text = "";
MessageBox.Show("Added! Click Move to see List Box");
}
else
{
MessageBox.Show("Already There!");
textBox1.Text = "";
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string idx = string.Empty;
for (int i1 = 0; i1 < checkedListBox1.CheckedIndices.Count; i1++)
idx += (string.IsNullOrEmpty(idx) ? string.Empty : ",") + Convert.ToString(checkedListBox1.CheckedIndices[i1]);
Properties.Settings.Default.CheckedItems = idx;
Properties.Settings.Default.Save();
}
#1
How are you opening the form? Is it something like:
你是如何打开表格的?是这样的:
FormName form = new FormName();
form.Show()
The only reason I can think that's happening is that you're instantiating a new form instance every time you show it, instead of reusing the same form.
我认为发生这种情况的唯一原因是,每次显示时都会实例化一个新的表单实例,而不是重复使用相同的表单。
#2
Have your Form take a ref List<string> values
as parameter. Then make this as BindingSource for the CheckedListBox.
让您的表单将ref List
Here is the code:
这是代码:
class MyForm : Form {
List<string> values;
BindingSource source;
public MyForm()
{
InitializeComponent();
}
public MyForm(ref List<string> values):this()
{
if (values == null)
values = new List<string>();
this.values = values;
checkedListBox1.DisplayMember = "Value";
checkedListBox1.ValueMember = "Value";
source = new BindingSource(this.values, null);
checkedListBox1.DataSource = source;
}
private void AddItemButton_Click(object sender, EventArgs e)
{
this.source.Add(textBox1.Text);
textBox1.Text = string.Empty;
}
}
#3
private void frmMain_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[] checkedIndicies = Properties.Settings.Default.CheckedItems.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i1 = 0; i1 < checkedIndicies.Length; i1++)
{
int idx;
if ((int.TryParse(checkedIndicies[i1], out idx)) && (checkedListBox1.Items.Count >= (idx+1)))
{
checkedListBox1.SetItemChecked(idx, true);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.MaxLength = 15;
// Change all text entered to be lowercase.
textBox1.CharacterCasing = CharacterCasing.Lower;
if (checkedListBox1.Items.Contains(textBox1.Text) == false)
{
checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);
textBox1.Text = "";
MessageBox.Show("Added! Click Move to see List Box");
}
else
{
MessageBox.Show("Already There!");
textBox1.Text = "";
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string idx = string.Empty;
for (int i1 = 0; i1 < checkedListBox1.CheckedIndices.Count; i1++)
idx += (string.IsNullOrEmpty(idx) ? string.Empty : ",") + Convert.ToString(checkedListBox1.CheckedIndices[i1]);
Properties.Settings.Default.CheckedItems = idx;
Properties.Settings.Default.Save();
}