简体   繁体   English

使用自定义属性保存设置

[英]Save settings with custom property

I have a setting which I have set to a custom property DataGridViewAutoSizeColumnsMode . 我有一个已设置为自定义属性DataGridViewAutoSizeColumnsMode I assume this is an ENUM if that has anything to do with it all. 我假设这是一个ENUM,如果与它有任何关系。

在此处输入图片说明

I have a method in my code which will get and set the property like this: 我的代码中有一个方法可以像这样获取和设置属性:

public DataGridViewAutoSizeColumnsMode COLUMN_SIZE_MODE
{
   get { return Properties.Settings.Default.COLUMN_SIZE_MODE; }
   set { Properties.Settings.Default.COLUMN_SIZE_MODE = value; }
}

Now, the setting is controlled by a comboBox with all the alternatives that DataGridViewAutoSizeColumnsMode contains, and it's ok reading from it. 现在,该设置由comboBox控制,其中包含DataGridViewAutoSizeColumnsMode包含的所有替代方案,并且可以从中读取。 However, I don't know how to save the selected value to the setting variable. 但是,我不知道如何将所选值保存到设置变量中。

When I press the "save" button the value chosen in the comboBox should be saved to the settings variable. 当我按下“保存”按钮时,在comboBox中选择的值应保存到设置变量中。 With other settings, which are just strings, I can just do like this: 使用其他设置(只是字符串),我可以这样做:

DEFAULT_DATABASE = defaultDatabaseComboBox.Text;

But as I have set the other setting to a non-text property I am unable to use the .Text method to point to the value in the comboBox like this: 但是,由于我将其他设置设置为非文本属性,因此无法使用.Text方法指向comboBox中的值,如下所示:

COLUMN_SIZE_MODE = columnSizeModeSetting.Text;

What can I do to save the text in my comboBox to the setting with a custom property? 如何将comboBox中的文本保存到具有自定义属性的设置中?

I just found a solution. 我刚刚找到了解决方案。 I don't really understand it yet but here it is. 我还不太了解,但是确实如此。

To save a setting from a comboBox, which contains a text, to a setting which is a custom enum property I did this: 要将设置从包含文本的comboBox保存到作为自定义枚举属性的设置,我这样做是:

COLUMN_SIZE_MODE = (DataGridViewAutoSizeColumnsMode)Enum.Parse( typeof(DataGridViewAutoSizeColumnsMode), columnSizeModeSetting.Text);

It seem to be a kind of conversion, but why I need to write like this I don't really understand yet. 这似乎是一种转换,但是为什么我需要这样写我还不太了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM