Hi, 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.
views:
439answers:
3
              +1 
              Q: 
              
            Issues adding items and expanding the collections list of checkedlistbox on a windows form in c#
                +2 
                A: 
                
                
              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.
                  GenericTypeTea
                   2009-07-06 13:51:01
                
              I use the event handlers form_load and form_closed.I have a program that repopulates checked items in a checkedlistbox list through the checkedindices property when I run a form so that the user doesn't have to recheck the boxes every time at run time.
                  
                   2009-07-06 14:01:27
                Yes, but how are you opening the form? Form_Load happens when you've opened the form and Form_Closed happens when you close it. Please post some code to give us a better example.
                  GenericTypeTea
                   2009-07-06 14:16:19
                
                +2 
                A: 
                
                
              
            Have your Form take a ref List<string> values as parameter. Then make this as BindingSource for the CheckedListBox.
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;
        }
}
                  Vivek
                   2009-07-06 13:55:43
                
              
                
                A: 
                
                
              
              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();
}
You might want to clean this up, it's quite unreadable. Edit the post and mark the code as code properly. Also, please show the code where you're Showing [i.e. .Show() or .ShowDialog()] the form.
                  GenericTypeTea
                   2009-07-07 07:48:59