Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is it possible to save a CHECKLISTBOX (not only a Checkbox) in the application settings ?

I want to retreive my checkboxes checked in my CheckListBox when i load my form ?
Posted
Comments
LanFanNinja 24-Nov-11 11:58am    
Check my solution

Well I am sure there are many ways but maybe this will help.

EDIT: All below changed to VB.Net for the OP.

First create a new property in your settings file
1) Right click your project in the solution explorer
2) Click on properties
3) Click on settings tab
4) Add a new property called CheckedListState and set its type to string

Then use below code (modify for your use)

VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim checkedStates As String() = My.MySettings.Default.CheckedListState.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

    For i As Integer = 0 To checkedStates.Length - 1
        CheckedListBox1.SetItemChecked(i, Boolean.Parse(checkedStates(i)))
    Next
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    My.MySettings.Default.CheckedListState = ""
    For i As Integer = 0 To CheckedListBox1.Items.Count - 1
        My.MySettings.Default.CheckedListState += CheckedListBox1.GetItemChecked(i).ToString() & ","
    Next

    My.MySettings.Default.Save()
End Sub



Ask me if you have any questions about this code and I will try to help you figure it out.
 
Share this answer
 
v5
Comments
contact97438 25-Nov-11 5:54am    
@LanFanNinja : Hello,

Thanks for the code.
First of all, im' not used with C#, is the code below correct ?

------
Private Sub Form1_Load(sender As Object, e As EventArgs)
Dim checkedStates As String() = Settings.[Default].CheckedListState.Split(New Char() {","C}, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = 0 To checkedStates.Length - 1
checkedListBox1.SetItemChecked(i, Boolean.Parse(checkedStates(i)))
Next
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs)
Settings.[Default].CheckedListState = ""
For i As Integer = 0 To checkedListBox1.Items.Count - 1
Settings.[Default].CheckedListState += checkedListBox1.GetItemChecked(i).ToString() + ","
Next

Settings.[Default].Save()
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik, @toddanglin
'Facebook: facebook.com/telerik
'=======================================================

2/. I never use the Settings.vb ; Is this correct ?

Namespace My

'Cette classe vous permet de gérer des événements spécifiques dans la classe de paramètres :
' L'événement SettingChanging est déclenché avant la modification d'une valeur de paramètre.
' L'événement PropertyChanged est déclenché après la modification d'une valeur de paramètre.
' L'événement SettingsLoaded est déclenché après le chargement des valeurs de paramètre.
' L'événement SettingsSaving est déclenché avant l'enregistrement des valeurs de paramètre.
Partial Friend NotInheritable Class MySettings

Public CheckedListState As String

End Class
End Namespace

3/. Here's my modifications :

LOAD FORM
Dim checkedStates As String() = My.Settings.CheckedListState.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = 0 To checkedStates.Length - 1
CheckedListBoxPJ.SetItemChecked(i, Boolean.Parse(checkedStates(i)))
Next

And FORM CLOSING :
Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

My.Settings.CheckedListState = ""
For i As Integer = 0 To CheckedListBoxPJ.Items.Count - 1
My.Settings.CheckedListState += CheckedListBoxPJ.GetItemChecked(i).ToString() + ","
Next

My.Settings.Save()


End Sub


4/. I've got a NullReferenceException on the .RemoveEmptyEntries
Do you know why ?

Thanks for your help
LanFanNinja 25-Nov-11 12:53pm    
Hello! Please recheck my solution I have changed it to VB.Net for you. I don't really have the time right now to give your code a fair read through. If your are still having problems with this code let me know and I will try to help.
contact97438 25-Nov-11 14:04pm    
@LanFanNinja : It doesn't work on the form Load.

I see things that are different, and an error on this line :

Dim checkedStates As String() = My.MySettings.Default.CheckedListState.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

When i enter My.MySettings.Default ("Default" is not accepted).

I put this, look if there's something wrong in it :

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim checkedStates As String() = My.Settings.CheckedListState.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = 0 To checkedStates.Length - 1
CheckedListBoxPJ.SetItemChecked(i, Boolean.Parse(checkedStates(i)))
Next
LanFanNinja 25-Nov-11 15:13pm    
I am not sure why My.MySettings.Default.CheckedListState is not working but
My.Settings.CheckedListState is the same so will work fine. Is this code not working?? Are you getting errors? If so post them.
contact97438 25-Nov-11 22:28pm    
@LanFanNinja :

Yes, i have a "NullReferenceException" error : "Object reference not set to an instance of an object." on this line of code :

Dim checkedStates As String() = My.Settings.CheckedListState.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

You don't save a control, you persist the controls state and values. Yes, it is possible to have this stored in you app.config
 
Share this answer
 
You Can't Save the Whole Control.You can Save the State of the CheckBoxList into the App.config file which stores the data in XML format from that you can check the State of CheckBox whether it is Checked or Not.
 
Share this answer
 
Comments
contact97438 24-Nov-11 9:49am    
OK, that's what i mean, but i don't know how to do that, cause i've got many members in my collection. I must create a field for all the checklistBox, or i must create a field for each item of the collection ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900