3

Suppose I have one form where a combo box has some options. Now, at the first run of this program, user selects a option from combo box and saved it through a button click or something. Now, If user terminates the application and run again for the 2nd time, is there any way to retrieve the last saved selection?

That means, if you select option1 from the combo box and terminate the application. after some time, you again start the application, now your combo box should show option1 as selected because at the previous session, you selected it.

I hope you'll understand what i think.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate - http://stackoverflow.com/q/8974707/799586 – Bali C Jan 26 '12 at 14:02
  • either by storing in the database or using the user settings framework of windows forms / .NET Framework. – Davide Piras Jan 26 '12 at 14:03
  • So do you already have a button that saves the data? If not, you can save to a file or database or something else. Then you read the value when you start the application. Have you attempted any of this yet? – musefan Jan 26 '12 at 14:03
  • possible duplicate of [What is the best way to store user settings for a .NET application?](http://stackoverflow.com/questions/26369/what-is-the-best-way-to-store-user-settings-for-a-net-application) – TrueWill Jan 26 '12 at 14:04
  • See also http://stackoverflow.com/questions/946431/whats-the-preferred-method-for-storing-user-settings-in-a-net-windows-app – TrueWill Jan 26 '12 at 14:04
  • @DavidePiras --Thanks a lot. But I am working on a project that don't allow me to use database. So, I can't use database. –  Jan 27 '12 at 01:08
  • This question is basically a dupe of http://stackoverflow.com/questions/26369/what-is-the-best-way-to-store-user-settings-for-a-net-application, but I think it's worth keeping because there's one quirk for saving combo boxes specifically: you can't store and restore the *text* value; you need to use the *index*. –  Nov 10 '15 at 15:49

3 Answers3

5

Use Settings

// To Load (after combo box binding / population)
private void LoadSelection()
{
    int selectedIndex = 0;

    if (int.TryParse(Properties.Settings.Default.comboBoxSelection, out selectedIndex))
    {
        cbMyComboBox.SelectedIndex = selectedIndex;
    }
}

// saving on button click.
private void saveButton_Click(object sender, EventArgs e)  
{  
    //set the new value of comboBoxSelection 
    Properties.Settings.Default.comboBoxSelection = cbMyComboBox.SelectedIndex;  

    //apply the changes to the settings file  
    Properties.Settings.Default.Save();  
}  

See here for more detail.

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • 1
    +1. Note that you don't need `TryParse`: settings are strongly-typed, so you can simply use an `int` setting and not worry about parsing a string. –  Nov 10 '15 at 15:48
1

You have to manually save the value and load it up again when the program starts.

The easy way to do it with Visual Studio is to create a Settings class. In VS, right click your project, click add new, scroll to "Settings File", add. VS will show you a UI where you can create new properties in the settings object that you can chose the name of.

If I create a new property called "ComboboxValue" of type string, I can reference it in the code as Settings1.Default.ComboboxValue = "hello world";

Here's the MSDN on it:

http://msdn.microsoft.com/en-us/library/a65txexh(v=vs.100).aspx

antiduh
  • 11,853
  • 4
  • 43
  • 66
0

You can add settings on solution explorer under the project, properties folder add resource "string" give it a name "selected" for example then

// this is save button
Properties.Settings.Default.selected = comboBox1.SelectedIndex;
Properties.Settings.Default.Save(); 

// this is retrieve (use it in window_load event for example)
comboBox1.SelectedIndex = Convert.ToInt32(Properties.Settings.Default.selected);
Alaa Jabre
  • 1,843
  • 5
  • 26
  • 52