Click on list object and display other multiple list objects in another listbox C#

Go To StackoverFlow.com

0

I am nearly to the answer but annoyingly, not quite. So far my code is:

   private void lstIndividuals_SelectedIndexChanged(object sender, EventArgs e)
   {
        var individual = lstIndividuals.SelectedItem as Individual;
        var tempSimilarFilesToFile1 = new HashSet<Individual>();
        int Counter = 0;
        foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1)
        {
            tempSimilarFilesToFile1 = new HashSet<Individual>();
            foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2)
            {
                if (kvpInd.Value.name.name.ToLower() == kvpInd2.Value.name.name.ToLower())
                {
                    Counter++;
                    similarInds.Add(kvpInd.Value);
                    if (Counter >= 1)
                    {
                        tempSimilarFilesToFile1.Add(kvpInd2.Value);
                    }
                }
            }
        lstIndividuals2.DataSource = tempSimilarFilesToFile1.ToList();
        lstIndividuals2.DisplayMember = "DisplayName";
        lstIndividuals2.ValueMember = "id";
   }

As you can probably see, the lstIndividuals2 listbox items are zooming through really fast. I would just like to click on an item in lstIndividuals

Then I would like that to display similar records found (anything that abides by the rule kvpInd.value.name.name == kvpInd2.value.name.name)

All similar items, I would like to be stored in tempSimilarFilesToFile1 and that to be the datasource for the lstIndividual2

I apologise if I have explained badly.

Thank you.

2012-04-05 01:42
by user1290653


0

You init the tempSimilarFilesToFile1 in the outer loop every time, so you actually get a list contains the items in _Individuals2 which are the same as the final item in _Individuals1. Just try to comment the init statement in the outer loop and see if that helps.

private void lstIndividuals_SelectedIndexChanged(object sender, EventArgs e)    
{
    var individual = lstIndividuals.SelectedItem as Individual;
    var tempSimilarFilesToFile1 = new HashSet<Individual>();
    int Counter = 0;
    foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1)
    {
        // comment the statement below
        //tempSimilarFilesToFile1 = new HashSet<Individual>();
        foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2)
        {
            if (kvpInd.Value.name.name.ToLower() == kvpInd2.Value.name.name.ToLower())
            {
                Counter++;
                similarInds.Add(kvpInd.Value);
                if (Counter >= 1)
                {
                    tempSimilarFilesToFile1.Add(kvpInd2.Value);
                }
            }
        }
    lstIndividuals2.DataSource = tempSimilarFilesToFile1.ToList();
    lstIndividuals2.DisplayMember = "DisplayName";
    lstIndividuals2.ValueMember = "id";
}
2012-04-05 05:46
by Kirin Yao
Ads