Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm struggling with this problem right now. I wanted to save the treeview state default to an application settings.

I have this code that saves all isChecked items to the list.
And i've also added "CheckedPaths" with string collection type, but when
I've tried to save it to the settings.default, it won't save, instead it gives an error that cannot save to string collection.

(PS. Sorry for this noob question but I'm still a beginner for this. Thanks :) )

C#
List<string> hashs = new List<string>();

        public bool? IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                SetIsChecked(_isChecked, true, false);
                if (_isChecked == true)
                {
                    hashs.Add(this.FullPathName);

                }
                else
                {
                    hashs.Remove(this.FullPathName);
                }
            }
}
Posted

StringCollection is a specific type. You cannot convert from List<t> to it. However, you don't really have to use StringCollection. That's just in the designer.

You have two choices
(1) Save your strings to StringCollection instead
(2) Change the auto-generated code in Settings.Designer.cs

I recommend the first, because changing auto-generated code can be problematic. The one issue I know is that you'll need to repeat the change anytime you add a setting through the designer.

The second option is simple to implement. Open the Settings.Designer.cs file and change the System.Collections.Specialized.StringCollection to System.Collections.Generic.List<string>. That's it, at that point you can write your normal code to manipulate the setting (it's not like you're going to use the designer anyway for the value).

C#
if (Settings.Default.TestSetting == null) {
   // create collection
   Settings.Default.TestSetting = myValues;
   Settings.Default.Save();
}

// do whatever
 
Share this answer
 
Comments
Silver Bel 2-Mar-15 2:46am    
Thank you for your quick response @tgrt :)

It works, I was able to save the path-names through stringcollection.


here's my code:

public bool? IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
SetIsChecked(_isChecked, true, false);
if (_isChecked == true)
{
if (UserSettings.Default.CheckedNodes == null)
{
UserSettings.Default.CheckedNodes = new System.Collections.Specialized.StringCollection();
}
UserSettings.Default.CheckedNodes.Add(this.FullPathName);
UserSettings.Default.Save();
}
else {
UserSettings.Default.CheckedNodes.Remove(this.FullPathName);
}
}
}



I've tested my code and i saw its working, but my problem now is: "Do you have any idea on how to retrieve the values in order to set the tree state during applications starts?

At first, I was thinking of doing it this way:

get { return _isChecked?? Usersettings.default.checkednodes; }
but because isChecked is boolean, i dont think it works.

or

do i need to create a new function to iterate the values in the settings and bind to the checkedboxes? :)
Thanks for the help! figured it out.


C#
System.Collections.Specialized.StringCollection _checkedPaths = CheckedList.CheckedLists.CheckPaths;
public bool? IsChecked
{
    get { return _isChecked; }
    set
    {
        _isChecked = value;
        SetIsChecked(_isChecked, true, false);
        if (_isChecked == true)
        {
                if (UserSettings.Default.CheckedNodes == null)
                {
                    UserSettings.Default.CheckedNodes = new System.Collections.Specialized.StringCollection();
                }

                if (CheckIfPathExists(this.FullPathName) == false)
                {
                    UserSettings.Default.CheckedNodes.Add(this.FullPathName);
                    UserSettings.Default.Save();
                }
        }
        else
        {
            UserSettings.Default.CheckedNodes.Remove(this.FullPathName);
            UserSettings.Default.Save();
        }
    }
}
}


and in my view model, I added this to my previous code that builds thee treeview.

C#
foreach (string _fileItem in UserSettings.Default.CheckedNodes)
                        {
                            if (UserSettings.Default.CheckedNodes.Contains(item.FullPathName))
                            {
                                item.IsChecked = true;
                            }
                        }
 
Share this answer
 
Comments
tgrt 2-Mar-15 8:12am    
Good deal. I'm glad you got it working.

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