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.
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);
}
}