I really have no idea how to figure this out, please help.
I have this filter button that is suppose to filter a listbox based on what is filled in a textbox and a list that is used to add the names the listbox
this is my code:
- public void FilterButton_Click(object sender, RoutedEventArgs e)
- {
-
-
-
- bool Agemin = int.TryParse(AgeBoxMin.Text, out int AgeMi);
-
-
- if (Agemin && AgeBoxMin.Text.Length != 0 && (AgeBoxMax.Text.Length == 0 || Agemax != true))
- {
- filtercount++;
- int T = 0; while (T < AgeListNumb.Count)
- {
- if (AgeListNumb[T] > AgeMi)
- {
- IndexAge.Add(T);
- TotalFilter.Add(T);
- }T++;}}
-
- if (JobTextBox.Text.Length != 0)
- { filtercount++;
- int P = 0; while (P < JobList1.Count)
- { if (JobList1[P].ToLower().Contains(JobTextBox.Text.ToLower()))
- {
- IndexJob.Add(P);
- TotalFilter.Add(P);
- }P++;}}
-
- (probable the problem)
- var matches = TotalFilter.GroupBy(x => x).Where(x => x.Count() == filtercount).Select(x => x.Key).ToList();
-
-
-
- if (filtercount != 0 && matches.Any())
- {
- AccountNamesList.Items.Clear();
-
- foreach (var I in matches)
- AccountNamesList.Items.Add(Listboxlist[I]);
- }
-
-
-
- }
The first time i run it it runs perfect just as i want it,
But the second time i use the above code it doubles matches
I have looked up what happend with the matches list:
- messageBox.Show(Matches.Aggregate("Matches indexes : ", (s1, s2) => s1 + " , " + s2);
If i use the first filter and i allow all items in
Listboxlist to pass( filling in 0 in AgeBoxMin(textbox))
it gives the output:
(If listboxlist would contain 6 items )
1,2,3,4,5,6
If i do it again
I get the output: 1,2,3,4,5,6,7,8,9,10,11,12
for some reason it doubles it and makes the entire program crash
because list box only contains 6 items not 12.
Maybe somehow it stores all the items in "var matches" and adds it all up.