0

As the title says, when the AfterLabelEdit fires, the ListViewItem gets stuck in edit mode even if I press return or click on empty space.

The error:

A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

This is what the code looks like:

    ListViewItem oldItem;

    private void listView_BeforeLabelEdit(object sender, LabelEditEventArgs e)
    {
        oldItem = listView.Items[e.Item]; // store the unedited item
    }

    private void listView_AfterLabelEdit(object sender, LabelEditEventArgs e)
    {
        // make sure the new label has been edited and isn't null
        if(e.Label != "" && e.Label != oldItem.Text)
        {
            // iterate through Dictionary<string, List<string>> dict, find values of the key named oldItem.Text
            foreach(var keyValuePair in dict)
            {
                if(keyValuePair.Key == oldItem.Text)
                {
                    foreach(string s in keyValuePair.Value)
                    {
                        if(s != "")
                            dict[e.Label].Add(s); // ----------------- Error
                    }
                }
            }
            dict.Remove(oldItem.Text);
        }
    }

I'm pretty sure the keys and values aren't null. Any ideas? Please ask if anything is still unclear.

betaFlux
  • 183
  • 1
  • 3
  • 12
  • 1
    My crystal ball says that you are running this code on the 64-bit version of Windows 7. That OS is pretty unfriendly to 32-bit code that raises exceptions like this. Change the project's Platform target to AnyCPU (prefer 32-bit off) so you get a much louder bang. And fix your code, e.Label is not yet in the dictionary since you just typed a different value. So a KeyNotFoundException is entirely expected. – Hans Passant Oct 01 '14 at 12:00
  • @HansPassant: Thanks for both, the tip with the Platform target and the solution. I stored the values in a list and assigned them to the dictionary after I created the dictionary key. If this comment can become an answer, I'd gladly accept it. Thanks again! – betaFlux Oct 01 '14 at 12:16

0 Answers0