Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save user settings programmatically?

Tags:

c#

.net

settings

I have a button which opens windows color pallet and then assigns select color to selected element in some virtual studio. Element is first selected by the user on mouse click, and based on element ID, color is assigned. So, each time the button is clicked, color of the same or different element is changed. Element ID is obtained from a delegate that fires if mouse clicked on some element. The code for color-set button is like this:

private void Btn_Choose_Color_Click(object sender, RoutedEventArgs e)
{
    uint id_selected = (uint)selected_element; //get id of selected element from clickintocallback

    //open windows color dialog
    System.Windows.Forms.ColorDialog my_dialog = new System.Windows.Forms.ColorDialog();
    my_dialog.ShowDialog();

    //get the color from windows dialog
    int red = my_dialog.Color.R;
    int green = my_dialog.Color.G;
    int blue = my_dialog.Color.B;

    //create cinector color object and pass rgb values from windows dialog
    ByteRGBColor desired_color = new ByteRGBColor((byte)red, (byte)green, (byte)blue); //assign color statically

    for (int i = 0; i < all_color_elements_in_loaded_studio.Count; i++)
    {
        uint id_current = all_color_elements_in_loaded_studio.ElementAt(0).colorElementID; //get id of current element in a loop

        if(id_current == id_selected) //compare selected and current element
        {
            //all_color_elements_in_loaded_studio.ElementAt(i).colorElementColor = test_color; //set the test color
            instance.SetStudioColorElement(id_current, desired_color); //assign the color to the element
            break;
        }
    }

    //after we choose a color
    Btn_Pick_Element_Clicked = false;
    Btn_Choose_Color.IsEnabled = false;
}

Now, my question is how to save element ID and its color once assigned into user settings? I understand that I can go to Properties->Settings and manually define user settings there, but here it must be done somehow programmatically. And then also, how to load these settings back?

like image 449
Ivan Avatar asked Dec 08 '22 02:12

Ivan


1 Answers

Set

Properties.Settings.Default.myColor = Color.AliceBlue;

Get

this.BackColor = Properties.Settings.Default.myColor;

Save

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();

Reference

like image 112
CharithJ Avatar answered Dec 10 '22 15:12

CharithJ