Search code examples
c#winformscomboboxapplicationsettingsbase

c# How to link combobox SelectedIndex to ApplicationSettings


In Visual Studio designer (I use 2012) there is no way to link ApplicationSettings mechanism to SelectedIndex property (only Text).

What workaround can be used instead?


Sorry if my question was against StackOverflow rules. I searched for my question and found no exact answers.

I don't know only how to custom code ApplicationSettings. I always used this feature in design mode - linked Text properties of my textboxes and then used Properties.Settings.Default.Save() to save this and on Form_Load I used Properties.Settings.Default.SomeName to load saved values. Everything besides this was done by VisualStudio and I don't know what exactly to change its behaviour for my needs.

I was sure that this question would be useful for starting programers


Solution

  • Write some code, it is as easy as using designer, besides, designer generates too much code which you don't understand.

    enter image description here

    Edit Settings in Visual Studio, add a property named "SelectedIndex", set its Type as int, and Scope as User, Value as 0 (meaning the 1st item is selected). And you can access this property in your code:

    public Form1()
    {
        InitializeComponent();
        comboxBox1.SelectedIndex = Properties.Settings.Default.SelectedIndex;
        this.Closing += Form1_Closing;
    }
    
    void Form1_Closing(object sender, CancelEventArgs e)
    {
        Properties.Settings.Default.SelectedIndex = comboxBox1.SelectedIndex;
        Properties.Settings.Default.Save();
    }