Search code examples
c#visual-studioenumspropertiesapplication-settings

Save settings with custom property


I have a setting which I have set to a custom property DataGridViewAutoSizeColumnsMode. I assume this is an ENUM if that has anything to do with it all.

enter image description here

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. 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. 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:

COLUMN_SIZE_MODE = columnSizeModeSetting.Text;

What can I do to save the text in my comboBox to the setting with a custom property?


Solution

  • 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:

    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.