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?
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.
ASKER
And how would I do that? Sorry, I am new to vb.net
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
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
ASKER
Bob,
How would I apply that to each index within the checkedlistbox?
Would I loop through the indices?
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
foreach checkbox In MyCheckboxObject
'check the setting for this checkbox and check if necessary
next
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?
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.SetItemChe cked(0, My.Settings.Checked1)
CheckedListBox1.SetItemChe cked(1, My.Settings.Checked2)
...
Bob
CheckedListBox1.SetItemChe
CheckedListBox1.SetItemChe
...
Bob
ASKER
There are about 50 of them.
then change it to:
foreach checkboxObj In MyCheckboxListObject
foreach checkboxObj In MyCheckboxListObject
ASKER
How do I store all 50 values in My.Settings using this format:
CheckedListBox1.SetItemChe cked(0, My.Settings.Checked1)
CheckedListBox1.SetItemChe cked(1, My.Settings.Checked2)
etc.
Do I have to set up settings for each item?
CheckedListBox1.SetItemChe
CheckedListBox1.SetItemChe
etc.
Do I have to set up settings for each item?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Bob
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?
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
Can they remove items?
Bob
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
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
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.CheckedListBox Settings.S plit(",")
Dim valueList() As String = setting.Split("=")
Dim name As String = valueList(0)
Dim value As String = valueList(1)
Dim index As Integer = CheckedListBox1.Items.Inde xOf(name)
If index <> -1 Then
CheckedListBox1.SetItemChe cked(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.Coun t - 1
sb.Append(CheckedListBox1. Items(inde x) & "=")
Dim ch As Char = IIf(CheckedListBox1.GetIte mChecked(i ndex), "1"c, "0"c)
sb.Append(ch & ",")
Next index
My.Settings.CheckedListBox Settings = sb.ToString().TrimEnd(",")
My.Settings.Save()
4) Initially the string won't have any value, and that should be OK.
Bob
1) Create a string with name+value pairs.
2) Set values from the setting:
For Each setting As String In My.Settings.CheckedListBox
Dim valueList() As String = setting.Split("=")
Dim name As String = valueList(0)
Dim value As String = valueList(1)
Dim index As Integer = CheckedListBox1.Items.Inde
If index <> -1 Then
CheckedListBox1.SetItemChe
End If
Next setting
3) Create and save the string:
Dim sb As New StringBuilder()
For index As Integer = 0 To CheckedListBox1.Items.Coun
sb.Append(CheckedListBox1.
Dim ch As Char = IIf(CheckedListBox1.GetIte
sb.Append(ch & ",")
Next index
My.Settings.CheckedListBox
My.Settings.Save()
4) Initially the string won't have any value, and that should be OK.
Bob