Link to home
Start Free TrialLog in
Avatar of tiehaze
tiehaze

asked on

Checkedlistbox in a windows application

I have a windows form with a checkedlistbox. When the user installs the windows application, I want all items in the checkedlistbox to be unselected. Once the user checks any of the items, I want it to store in the program so that the next time the user opens it, the checked items will remain checked. Anyone know how I can do this?
Avatar of strickdd
strickdd
Flag of United States of America image

I believe .Net has a Settings.Settings file that you can save you information to. You need to set up the file with keys and values and then you can make it so the code can modify this file. It is essentially a configuration file. Then on the load of the form, you can check this file and see which values are set and pre-select those checkboxes.
Avatar of tiehaze
tiehaze

ASKER

And how would I do that? Sorry, I am new to vb.net
Avatar of Bob Learned
You can save settings in My.Settings, and then retrieve them later:

1) Double-click on the 'My Project' entry in the Solution Explorer

2) Select the 'Settings' tab.

3) Add a 'User' setting as a Boolean type with the value = False

4) Examples:

  Add a setting called 'Checked1'

  ' Retrieve the value
  Dim wasChecked As Boolean = My.Settings.Checked1

  ' Change and save the value
    My.Settings.Checked1 = True
    My.Settings.Save()

Bob

Avatar of tiehaze

ASKER

Bob,

How would I apply that to each index within the checkedlistbox?

Would I loop through the indices?
Yeah, you would have to loop through your indexes:

foreach checkbox In MyCheckboxObject

   'check the setting for this checkbox and check if necessary

next
Avatar of tiehaze

ASKER

It is giving me the error " 'CheckBox' is a type and cannot be used as an expression"

Any ideas what I need to do?
How many items do you have in the CheckedListBox?  If there aren't too many, you could just do something like this:

    CheckedListBox1.SetItemChecked(0, My.Settings.Checked1)
    CheckedListBox1.SetItemChecked(1, My.Settings.Checked2)
    ...

Bob
Avatar of tiehaze

ASKER

There are about 50 of them.
then change it to:

foreach checkboxObj In MyCheckboxListObject
Avatar of tiehaze

ASKER

How do I store all 50 values in My.Settings using this format:

CheckedListBox1.SetItemChecked(0, My.Settings.Checked1)
CheckedListBox1.SetItemChecked(1, My.Settings.Checked2)
etc.

Do I have to set up settings for each item?

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tiehaze

ASKER

Thanks, that would work perfect but the user is going to have the capability to add or remove items from the checked listbox. Is there anyway to do this?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My solution should work for any number of checkboxes.
Sure, I used a StringBuilder to build a string of 1's and 0's as flags to indicate the state of each item's checked state.

Bob
Avatar of tiehaze

ASKER

Bob,

So for my example, if the settings is set up with 50 zeros, the user goes in and adds 5 items, will your solution still work even though the new string will be 55 characters long?
Ok, now you've done it!!  *GRIN*

Can they remove items?

Bob
Avatar of tiehaze

ASKER

No, they cannot
Will items only be added to the end of the list?  

Here's the problem--if they are added somewhere in the middle, then the saved values won't match.  

If the new items can only be added to the end, then you would still match up with the existing items, and the string would just grow longer to accomodate the new items.

Bob
Avatar of tiehaze

ASKER

I set up the CheckedListBox to be sorted alphabetically. What if I set it up so that if the user adds an item, it rebuilds the string, and therefore will match. Will that work?
Suggestion #3 (this may get bulky, but who cares!!!)

1) Create a string with name+value pairs.

2) Set values from the setting:
 
    For Each setting As String In My.Settings.CheckedListBoxSettings.Split(",")
      Dim valueList() As String = setting.Split("=")
      Dim name As String = valueList(0)
      Dim value As String = valueList(1)

      Dim index As Integer = CheckedListBox1.Items.IndexOf(name)

      If index <> -1 Then
        CheckedListBox1.SetItemChecked(index, (value = "1"))
      End If
    Next setting

3) Create and save the string:

    Dim sb As New StringBuilder()
    For index As Integer = 0 To CheckedListBox1.Items.Count - 1
      sb.Append(CheckedListBox1.Items(index) & "=")
      Dim ch As Char = IIf(CheckedListBox1.GetItemChecked(index), "1"c, "0"c)
      sb.Append(ch & ",")
    Next index

    My.Settings.CheckedListBoxSettings = sb.ToString().TrimEnd(",")
    My.Settings.Save()

4) Initially the string won't have any value, and that should be OK.

Bob