-1

Specific Problem I want to update KeyValue Pair, if I delete something , it should get deleted and remaining items gets saved, but when I delete a key, every value gets deleted. I just want to delete a specific value and retain the remaining values and save it back.

Edited: Obviously if I delete a key every value will get deleted, I want to delete a specific value from the key and retain remaining values from key.

Note: if my question is wrong you can suggest edits.

A Little Background:

1st ComboBox has category A and 2nd comboBox has category B. when I select category A, I generate some serials and at the same time I add to comboBox2 for display and add to keyvalue pair.

same with category B.

when I select category A, I only want to see generated serials in category A and when I select category B, I only want to see generated serials in category B.

This is why I'm using KeyValuePair, i could not find any other better solution, I save the file with json and when I load the application I reload the file and repopulate my comboBoxes with the data.

now I want to use a specific serial, when the serial is used, I want to remove it from the comboBox and also remove from the keyvalue pair and save back existing data.

here is the picture to understand it better. Products Serial Generator

Global Variables:

    List<KeyValuePair<int, string>> vals = new List<KeyValuePair<int, string>>();
    int from;
    int to;
    int result;

Following is the code to add List.

Add to comboBox and add to keyvalue pair.

 for (int i = 0; i <= result; i++)
     {
       string item = Convert.ToString(from + i);
       comboBox1.Items.Add(item);
       vals.Add(new KeyValuePair<int, string>(0, item));
     }

Save Code:

  string json = JsonConvert.SerializeObject(vals);
        using (StreamWriter sw = File.CreateText("C:\\Dictionary.txt"))
        {
            sw.Write(json);
        }   

Repopulating comboBox

string jsonToRead = File.ReadAllText("c:\\Dictionary.txt");

        List<KeyValuePair<int, string>> myDictionaryReconstructed = 
            JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);

        if (comboBox1.SelectedIndex == 0)
        {
            comboBox2.Items.Clear();
            foreach (var item in myDictionaryReconstructed)
            {
                if (item.Key == 0) //If Key value is 0, if it is CategoryA
                {
                    comboBox2.Items.Add(item.Value);
                    comboBox2.Refresh();
                }
            }

How to Delete Key & Save back remaining data

// this is probably wrong way of doing it. // I want to delete that value that is selected in comboBox2

   private void DeleteButton_Click(object sender, EventArgs e)
    {
   // Deserialise it from Disk back to a Dictionary
        string jsonToRead = File.ReadAllText("c:\\Dictionary.txt");

        List<KeyValuePair<int, string>> myDictionaryReconstructed =
            JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);

        foreach (var item in myDictionaryReconstructed)
        {
            if (item.Key == 0) //If Key value is 0, if it is CategoryA
            {
                if (item.Value == this.comboBox1.SelectedValue.ToString())
                {
                    vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedValue.ToString()));
                }
            }

        }

        string json = JsonConvert.SerializeObject(myDictionaryReconstructed);
        using (StreamWriter sw = File.CreateText("C:\\Dictionary.txt"))
        {
            sw.Write(json);
        }


    }
Patrick
  • 217
  • 1
  • 5
  • 19
  • `vals.Add(new KeyValuePair(0, item));` You always set the key pair as (0, something)? – Raskayu Aug 05 '16 at 07:20
  • For category A I'm using 0 and for category B I'm using 1. `vals.Remove(new KeyValuePair(0, comboBox2.SelectedItem.ToString()));` using this deletes entire key, instead of just value. – Patrick Aug 05 '16 at 07:36
  • Try using `IndexOf()` to get where the element is and `RemoveAt()` of vals, so you force deleting just one for sure – Raskayu Aug 05 '16 at 08:59
  • 1
    Remove actually works fine. I think it's something related with the foreach loop which seemed rather unnecessary to me. @Patrick Have you actually debugged the behavior? Could you please try removing foreach block in `DeleteButton_Click` event and replace it with `vals.Remove(new KeyValuePair(0, comboBox1.SelectedItem.ToString()));` ? – uTeisT Aug 05 '16 at 09:12
  • @uteist I removed for-loop and it worked, only comboBox did not get updated il check that... – Patrick Aug 05 '16 at 10:34
  • I set comboBox2.SelectedIndex = -1, after removing value and it worked. Thanks uteist. – Patrick Aug 05 '16 at 10:42
  • @Patrick I just posted it as an answer. Could u please mark it, if it helped? Ty – uTeisT Aug 05 '16 at 11:05

1 Answers1

1

I think it's something related with the foreach loop which seemed rather unnecessary to me.

Could you please try removing foreach block in

DeleteButton_Click 

event and replace it with

vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedItem.ToString()));
uTeisT
  • 2,256
  • 14
  • 26