1.11M Members

checkedlistbox save checked items for future

 
0
 

Hi friends,

I have a checkedlistbox control on my windows form. Now when Application runs, every item is unchecked and user checks the items as per his choice. I want that - next time when user runs the application - checkedlistbox should be loaded with the same items checked. Meaning no need to recheck those values.

How do i do that?

many thanks,
Jeet

 
0
 

Hi friends,

I have a checkedlistbox control on my windows form. Now when Application runs, every item is unchecked and user checks the items as per his choice. I want that - next time when user runs the application - checkedlistbox should be loaded with the same items checked. Meaning no need to recheck those values.

How do i do that?

many thanks,
Jeet

Store the value or status of checkboxlist items into database or file (text file) or an xml file.

 
0
 

You can also use the configuration file of the assembly to store the value for each local user on the machine which sounds like it would be the most fitting in this case.

Add a property called "CheckedItems" as a string to your Settings.Settings file and wire up these events in your form:

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 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 can serialize an arraylist of integers for the checked indexes which would be the "proper" way to do it I suppose, but this will work.

 
0
 

That was EXACTLY what I was looking for. Thank you! :)

 
0
 

Welcome Babushka.


I'm glad you got it helpful. Please do not resurrect old threads.If you have any questions please ask. You are welcome to start your own threads.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.

 
0
 

Welcome Babushka.


I'm glad you got it helpful. Please do not resurrect old threads.If you have any questions please ask. You are welcome to start your own threads.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article