0

I have a program with a combo box, a list for that box and a string.

The string is created from user input via a text box and is saved each time the program closes. So if I type "Input Text" then close and open the program the combo box will have a list containing 1 string which displays "Input Text"

The issue is I want to keep adding to the list with new information but at the moment its just constantly overriding what I put in last time.

How do I add to my list with a new item each time the string is different?

private void Form1_Load(object sender, EventArgs e)
{
    //Load Settings
    saveLocationTextBox.Text = MySettings.Default["SaveSaveLocationText"].ToString();

    List<string> list = new List<string>();
    list.Add(saveLocationTextBox.Text);

    comboBox.DataSource = list;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //Save User Input
    MySettings.Default["SaveSaveLocationText"] = saveLocationTextBox.Text;
    MySettings.Default.Save();
}
2
  • 1
    Try putting the list in a separate file Commented Aug 19, 2016 at 10:05
  • 1
    You need to save the existing list of strings assigned to the combobox as data source, and in Form1_Load you should add the new string to that saved list of strings rather than creating a new list and adding it into that list. Because each time you are creating a new list from scratch and you only add the new string into it. Commented Aug 19, 2016 at 10:05

2 Answers 2

1

The problem is that you are saving only the selected item text in your settings, not all the items in the combo box.

I suggest to get all the items in the combo first and put them in a comma delimited string.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //Save User Input
    string[] itemsToStore = new string[comboBox.Items.Count];

    for (int i = 0; i < comboBox.Items.Count; i++)
    {
        itemsToStore[i] = comboBox.GetItemText(comboBox.Items[i]); 
    }

    MySettings.Default["SaveSaveLocationText"] = saveLocationTextBox.Text;
    MySettings.Default["SaveSaveLocationItems"] = string.Join(",", itemsToStore);
    MySettings.Default.Save();
}

private void Form1_Load(object sender, EventArgs e)
{
    //Load Settings
    saveLocationTextBox.Text = MySettings.Default["SaveSaveLocationText"].ToString();

    string listItems = MySettings.Default["SaveSaveLocationItems"].ToString();

    List<string> list = new List<string>();
    list.AddRange(listItems.Split(','));

    comboBox.DataSource = list;
}
Sign up to request clarification or add additional context in comments.

Comments

1

instead of overriding the text in the file do:

var items = comboBox.Items.Select(item => comboBox.GetItemText(item)).ToList();
items.Add(saveLocationTextBox.Text);

MySettings.Default["SaveSaveLocationText"] = string.Join(";", items);

And then when reading it:

var text = MySettings.Default["SaveSaveLocationText"].ToString();
comboBox.DataSource = text.Split(';').ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.