将CheckedListBox项目保存到设置 [英] Save CheckedListBox Items to Settings

查看:287
本文介绍了将CheckedListBox项目保存到设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户设置的 CheckedListBox 中维护已检查项目的列表,然后在应用程序加载时重新加载它们。

I am trying to maintain a list of checked items in a CheckedListBox in User Settings and then reload them upon Application load.

在我的 Settings.settings 文件中,添加了以下内容:

In my Settings.settings file, I added the following:

<Setting Name="obj" Type="System.Windows.Forms.CheckedListBox.ObjectCollection" Scope="User">
    <Value Profile="(Default)" />
</Setting>

然后,在 chkList_ItemCheck 上,以下内容:

And, on chkList_ItemCheck, I am doing the following:

Properties.Settings.Default.obj = chkList.Items;
Properties.Settings.Default.Save()

但是由于某些原因,当我退出时应用程序,重新打开,并检查 Properties.Settings.Default.obj 的值,它是 null

But for some reason, when I exit the app, re-open, and check the value of Properties.Settings.Default.obj, it is null.

我在做什么错/想念?

推荐答案

code> CheckedListBox.CheckedItems 属性无法绑定到设置,您应该添加一个字符串属性,并将选中的项目存储为设置中的逗号分隔字符串,并在关闭表单并设置选中的项目时保存设置加载时的CheckedListBox。

Since CheckedListBox.CheckedItems property in not bindable to settings, you should add a string property and store checked items as comma separated string in settings and save settings when closing the form and set checked items of CheckedListBox at form Load.

要保存 CheckedListBox CheckedItems c>:

To save CheckedItems of a CheckedListBox:


  1. 在<$ c $中向您的项目添加设置文件c> Properties 文件夹,或者如果打开了该文件夹。

  2. 添加一个名为 CheckedItems

  3. 在表单的 Load 事件中,从设置中读取选中的项目,并使用在CheckedListBox中设置选中的项目SetItemChecked

  4. FormClosing 事件中,读取 CheckedItems of CheckedListBox并保存为设置,以逗号分隔的字符串。

  1. Add a Settings file to your project in Properties folder, or if you have that open it.
  2. Add a string setting property named CheckedItems
  3. In Load event of form, read checked items from settings, and set checked items in CheckedListBox using SetItemChecked.
  4. In FormClosing event, read CheckedItems of CheckedListBox and save in setting as comma separated string.

代码:

private void Form1_Load(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
    {
        Properties.Settings.Default.CheckedItems.Split(',')
            .ToList()
            .ForEach(item =>
            {
                var index = this.checkedListBox1.Items.IndexOf(item);
                this.checkedListBox1.SetItemChecked(index, true);
            });
    }


}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    var indices = this.checkedListBox1.CheckedItems.Cast<string>()
        .ToArray();

    Properties.Settings.Default.CheckedItems = string.Join(",", indices);
    Properties.Settings.Default.Save();
}






如何做我知道它不绑定设置吗?

作为第一个证据,对于设计器模式下的每个控件,在属性网格中,您都可以选中+( ApplicationSettings )( PropertyBinding )[...]查看支持属性绑定的属性列表。

As first evidence, for each control in designer mode, in property grid, you can check +(ApplicationSettings) (PropertyBinding)[...] to see a list of properties that support property binding.

此外,您应该知道 应用程序设置使用Windows Forms数据绑定体系结构来提供设置对象和组件之间的设置更新的双向通信。 [1]

Furthermore, you should know "Application settings uses the Windows Forms data binding architecture to provide two-way communication of settings updates between the settings object and components."[1]

例如,当您绑定 BackColor CheckedListBox 的c>属性更改为 MyBackColor 属性,这是Designer为其生成的代码:

For example when you bind BackColor property of your CheckedListBox to a MyBackColor property, here is the code that the Designer generates for it:

this.checkedListBox1.DataBindings.Add(
    new System.Windows.Forms.Binding("BackColor", 
    global::StackSamplesCS.Properties.Settings.Default, "MyBackColor", true,
    System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

现在让我们看一下属性定义 [2]

Now let's look at property definition [2]:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CheckedListBox.CheckedItemCollection CheckedItems { get; }




属性或索引器 CheckedListBox.CheckedItems 无法分配给
,因为它是只读的

The property or indexer CheckedListBox.CheckedItems cannot be assigned to because it is read only

有没有更好的选择?

此属性的简短答案是否。

The short answer for this property is No.

假设我们有一个属性 MyCheckedItems

[UserScopedSetting()]
public CheckedListBox.CheckedItemCollection MyCheckedItems
{
    get
    {
        return ((CheckedListBox.CheckedItemCollection)this["MyCheckedItems"]);
    }
    set
    {
        this["MyCheckedItems"] = (CheckedListBox.CheckedItemCollection)value;
    }
}

我们或设置提供商如何实例化 CheckedListBox.CheckedItemCollection

How can we or setting provider instantiate CheckedListBox.CheckedItemCollection?


类型 CheckedListBox.CheckedItemCollection 没有定义公共的
构造函数

The type CheckedListBox.CheckedItemCollection has no public constructors defined

因此我们无法实例化它。 CheckedListBox内部使用的唯一构造函数是 [2]

So we can not instantiate it. The only constructor that CheckedListBox internally use it is [2]:

internal CheckedItemCollection(CheckedListBox owner)

此外,向此集合添加项目的唯一方法是 CheckedItemCollection 的内部方法:

Furthermore the only way to add an item to this collection is an internal method of CheckedItemCollection:

internal void SetCheckedState(int index, CheckState value)

因此,我们无法对此属性进行更好的解决。

So we can't do a better workaround for this property.

更多信息:


  • 对于设计器模式下的每个控件,在属性网格中,您都可以选中+( ApplicationSettings )( PropertyBinding )[...]查看支持属性绑定的属性列表。

  • 设置序列化的工作方式是,它首先尝试调用 ConvertToString ConvertFromString TypeConverter 上的c>。如果此操作不成功,则使用XML序列化。 [1] 因此,如果您没有只读属性或缺少构造函数的类,可以使用自定义 TypeConverter 或自定义序列化程序。

  • For each control in designer mode, in property grid, you can check +(ApplicationSettings) (PropertyBinding)[...] to see a list of properties that support property binding.
  • Setting serialization works this way, it first attempts to call the ConvertToString or ConvertFromString on the type's associated TypeConverter. If this does not succeed, it uses XML serialization instead. [1] So For cases that you are not faced with read only properties or constructor-less classes, you can serialize value using a custom TypeConverter or a custom serializer.

这篇关于将CheckedListBox项目保存到设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆