Clearing Multi-Selected Items From Listbox C# - c#

What i've tried:
try 1:
for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
{
int idx = listBox1.SelectedIndices[x];
listBox2.Items.Add(listBox1.Items[idx]);
listBox1.Items.RemoveAt(idx);
}
try 2:
ArrayList tmpArr = new ArrayList();
foreach (object obj in listBox1.SelectedItems)
{
listBox2.Items.Add(obj);
tmpArr.Add(obj);
}
foreach (object obj in tmpArr.ToArray())
{
listBox1.Items.Remove(obj);
}
Also tried everything in the following post: How to remove multiple selected items in ListBox?
Still nothing worked. What am I doing wrong?

var selectedItems = new object[listBox1.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(selectedItems, 0);
foreach (var item in selectedItems)
{
listBox1.Items.Remove(item);
}
or with a bit of LINQ to simplify the code:
foreach (var item in listBox1.SelectedItems.Cast<object>().ToArray())
{
listBox1.Items.Remove(item);
}
The reasoning here is that you get all the selected items and put them into another list first. The original issue is that any change you make to the ListBox will change things like SelectedItems and SelectedIndices. Once you've created an independent array and put the selected items into it, nothing you do to the ListBox will affect that array so you can just enumerate it normally.

listbox1.BeginUpdate();
for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
{
int idx = listBox1.SelectedIndices[x];
listBox1.Items.RemoveAt(idx);
}
listbox1.EndUpdate();
If you cannot guarantee that every object in the list is unique, then this is the correct way to do it, to ensure that the correct selected items get removed.
If you have multiples of the same object in your listbox, you have to refer to them by "index", otherwise if you remove them by "item" it will remove the first instance of any matching items it finds.
I am in the process of writing a bus route planner which called for replication of the waypoint markers in the list. These were stored as strings, so for example I might have had "w1", "w2", "w3"... "w2" (think of a bus going down a high street, looping round a couple of blocks and then returning down the other side to understand why I have that... I only need waypoint markers in the centre of the road, not in each lane)
If I had selected the last "w2" marker as part of a range and used the selectedItem() method to to remove them, it would have removed the first "w2", not the second one. By using the SelectedIndex() method, it removes based on position, not value, so duplicate values are left safely intact.
I just wanted to add that as I have just been dealing with this very same problem, so saw first hand the problem removing by SelectedItem() caused.

Related

Way to delete row from list if criteria met

i am running two lists through a fuzzy matching program and want to be able to remove that row from the list if a match occurs but am having problems doing so. here is my code
foreach (var name in list)
{
foreach (var stepone in Step1)
{
if (FuzzyMatching(name.Full(), stepone.Full()) >= 90)
{
csvcontent.AppendLine(name.Full());
//find a way to delete list record out of list
//list.Remove(name);
}
}
}
you can mark in another list all the items to delete.
When the loop is finished you can delete all the items in the new created list from that list.
With LINQ is simply:
list1 = list1.Where(x => !itemToDeleteList.Contains(x)).ToList();
You can't remove an item from a list using a ForEach loop. In this case you should use a for loop.
var list = new List<string>();
list.Add("a");
list.Add("b");
list.Add("c");
list.Add("d");
for(int i = 0; i < list.Count(); i++)
{
if(list[i].Contains("a"))
{
list.RemoveAt(i);
}
}
Your results list will be:
b
c
d
Leaving the above for reference, but to #LarsTech comment below, list.RemoveAt breaks the loop so it will only remove 1 item. If you are wanting to remove multiple items, Alex's other answer with the Linq would be the cleanest way to remove it.
If you don't want to create a second list to house the items to be deleted, you can also do the below:
for (int i = 0; i < list.Count(); i++)
{
if (list[i].Contains("a"))
{
list = list.Where(x => !x.Contains("a")).ToList();
}
}
When you loop over a list with foreach and try to remove an item, you will get a runtime InvalidOperationException:
Collection was modified, enumeration operation may not execute
So you can use a for loop, which can handle modifications to the list. However, when you delete an item, all higher indexes will shift down. This will mean you skip an item.
The solution is to iterate in reverse order:
var list = new List<string>();
list.Add("a");
list.Add("b");
list.Add("b");
list.Add("c");
list.Add("d");
for (int i = list.Count()-1; i>=0; i--)
{
if (list[i].Contains("b"))
{
list.RemoveAt(i);
}
}
// result: a, c, d
you have to start at "count - 1", because that is the highest actual index
the last index to use is 0 (the start of the list)
and decrease the index after every iteration

C#: How do I iterate through two listBoxes individually?

I am trying to iterate through two listBoxes for a program I am coding. Both listBoxes will have a different item count inside of it.
Basically, I want my program to get the selectedItem from one listBox and use the string or text from that item to replace the text from EACH and EVERY single item in the other listBox.
Once it's done using the selectedItem from the original listBox for all the items in the other listBox, I want it to go to the next item in the original listBox and do the same process all over again.
It should repeat this UNTIL it has gone through ALL of the items in the original listBox.
Hopefully that made sense....
Here is some example code I made. I created two for loops so that it could iterate through both listBoxes.
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
listBoxNewListBox.SelectedIndex = 0;
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex++;
}
}
My issue is, when the inner for loop finishes iterating completely it errors out. I know the error is because it reached the end of the listBox and can't go any further, but I don't know how else to iterate through the listBox without having the items selected.
What it should do is, once it reaches the end of "listBoxNewListBox" it should go to the next item in "listBoxOriginal", and perform the same process all over again until it's done going through every item in "listBoxOriginal".
Any help would be appreciated!
I think problem is because of SelectedIndex at list. I think it's just going to far.
Here is a little modification:
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex = o;
}
}
Here is explanation:
When in inner loop you are doing this operation: listBoxNewListBox.SelectedIndex++ you are setting this index as 1 more than index of loop. That means, if we look at very last iteration of inner loop, this index is set with value which is already to high. This is probably reason why application throws an exception.

removing multiple list items from a listview

So I have created a button that when pressed it deletes the multiple items in the listview. However, I can only seem to make it delete one list item at a time. I know this is badly worded so my code should help explain what I am doing:
private void button2_Click(object sender, EventArgs e) //remove
{
try
{
foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
task.RemoveAt(listView1.SelectedItems[0].Index); // Remove task from the list "task"
I am trying to make it delete all selected items from my listview as well as my list named "task" However, the line of code above only allows me to delete one selected item from the list at a time.
Any help? Thanks
Try This,
for (int i = task.SelectedItems.Count - 1; i >= 0; i++)
{
if (task.Items[i].Selected)
{
task.Items.Remove(listView2.Items[i]);
}
}
I'm going to assume that your list of tasks is equal, in item count, to your ListView, and that the object in task[0] is the same as ListView.Items[0]. If that is true, remove the item from the task first then remove the item from the ListView
// Removing items from the bottom of the selected items and working your way up
for (int i = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
task.RemoveAt(listView1.SelectedIndices[i]);
listView1.Items.RemoveAt(listView1.SelectedIndices[i];
}
foreach (ListViewItem eachItem in listView1.Items)
{
listView1.Items.Remove(eachItem);
}
you can only remove one item per line,
you can use the ".ToArray" extension method, if you are enumerating over a changing enumerable.
you should also use the BeginUpdate / EndUpdate function, to speed it up
https://msdn.microsoft.com/library/system.windows.forms.listview.beginupdate%28v=vs.110%29.aspx

Loop through wpf listbox using a for loop instead of a foreach loop

I have a c# wpf listbox and I am trying to get the values from the selected items. I cannot use a foreach loop (every value I find will remove an item from the listbox). But this seems impossible.
What I want is somthing like this:
for (int i = <numberofselecteditems> - 1; i >= 0; i--)
{
string displaymembervalue = listbox.selecteditem[i].displaymembervalue;
}
I have a solution which involve to loop over all the listbox items twice. This is not really an option since it will slow the app too much.
Like I said before, this is NOT the
System.Windows.Forms.Listbox
but the
System.Windows.Controls.Listbox
thank you!!
J.
See the solution here, it is essentially using a foreach in the follolwing fashion:
foreach (var item in listBox1.SelectedItems)
{
// Do what you want here... Console.WriteLine(item), etc.
}
If you really want to do it with a for loop rather than a foreach, then do the following:
for(int i = selectedItems.Count - 1; i >= 0; --i)
{
var item = selectedItems[i];
// Do what you want with item
}
Here is your XAML bound to a Observable collection
<ListBox ItemsSource="{Binding items}"/>
Here is your observable collection of objects
private ObservableCollection<Object> _items;
public ObservableCollection<Object> items{
get{ return _items; }
}
Here is the enumeration over them and the removing of each item
for(int x = 0; x < _items.Count; x++){
_items.Remove(_items.Where(n => n == _items[x]).Single());
//You may have to do a notify property changed on this if the UI Doesnt update but thats easily googled.
//Traditionally it would update. However since you are bound to items Im not sure if it will update when you manipulate _items
}
Create a second list. You still have to iterate twice, but the second iteration is not over the entire list of items.
var items List<ListBoxItem>;
foreach (var item in listbox1.SelectedItems)
items.Add(item);
foreach (var item in items)
listbox1.Remove(item);
Alternatively instead of enumerating twice you can create a copy of the list of objects and then remove the items from the original list while still enumerating.
foreach (var selectedItem in listBox1.SelectedItems.Cast<List>())
{
//remove items from the original list here
}

How to delete duplicate items in listview?

Well, I need to check if there exists duplicate items inside listview on my app, but... I don't know how.
The way to detect this is by checking the field "Tag", if they are the same, then delete the item.
A good way to find duplicates is to use a temporary hashset. This gives you an O(n) O(n log n) algorithm (see Rick Sladkeys comments) to detect duplicates. Example:
var tags = new HashSet<string>();
var duplicates = new List<Item>();
foreach(Item item in listView.Items)
{
// HashSet.Add() returns false if it already contains the key.
if(!tags.Add(item.Tag)
duplicates.Add(item);
}
[Remove duplicates here]
Used nested for loops to go through and check each item against each other item.
//tag duplicates for removal
List<Item> toRemove = new List<Item>();
foreach(Item item1 in listView.Items)
{
foreach(Item item2 in listView.Items)
{
//compare the two items
if(item1.Tag == item2.Tag)
toRemove.Add(item2);
}
}
//remove duplicates
foreach(Item item in toRemove)
{
listView.Items.Remove(item);
}
You'll have to tweak the syntax for your code, but that's the basic idea behind it. Also, there are optimizations that could probably be made to it, but don't worry about those yet.
None of the above helped me, so I thought I'd post what I came up with in case someone else is having this issue.
myListView is a ListView, sorted alphabetically (so duplicates in this case are adjacent). You could sort it programatically beforehand if you wanted.
myListView.Sorting = SortOrder.Ascending;
Anyway this is fairly simplistic but I hope it helps someone!
for (int i = 0; i < myListView.Items.Count - 1; i++)
{
if (myListView.Items[i].Tag == myListVIew.Items[i + 1].Tag)
{
myListView.Items[i + 1].Remove();
}
}
If you have a lot of items then you can use a HashSet to keep the performance acceptable.
Loop over the items starting with the HashSet empty. For each item check if the tag is in the HashSet. If so this is a duplicate. If not add the tag to the HashSet.
This approach avoids an N^2 algorithm which is what you get with a nested loop. The HashSet makes the algorithm linear complexity providing the removal phase is carefully implemented. Of course this may not matter to you depending on how many items you have. If the list is small use nested loops and keep it simple.
If you are comparing duplicated Items based on specific columns you should simply use:
if(listview.Items.Find(SearchQuery).Count > 0)
{
//remove duplicates from list...
}
but you should name every item in the list when you fill up the listview..
for example if I want to search for duplicate items in column 1+2+5 I'd name the whole row like:
myItemName.Name = column1.Text + "_" + column2.Text + "_" + column5.Text;
and then perform the above "if statement"..
I would like to offer my solution using a Dictionary for future reference:
(you will edit for your own "Colums" (subitems), Simply read the code to understand.
I used the below to remove "Dups" in a listview on a button click, i am searching subitem you can edit code for your own use...
uses dictionary and a little easy "update" class i wrote.
private void removeDupBtn_Click(object sender, EventArgs e)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
int num = 0;
while (num <= listView1.Items.Count)
{
if (num == listView1.Items.Count)
{
break;
}
if (dict.ContainsKey(listView1.Items[num].SubItems[1].Text).Equals(false))
{
dict.Add(listView1.Items[num].SubItems[1].Text, ListView1.Items[num].SubItems[0].Text);
}
num++;
}
updateList(dict, listView1);
}
and using a little updateList() class...
private void updateList(Dictionary<string, string> dict, ListView list)
{
#region Sort
list.Items.Clear();
string[] arrays = dict.Keys.ToArray();
int num = 0;
while (num <= dict.Count)
{
if (num == dict.Count)
{
break;
}
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
lvi = new ListViewItem();
lvi.Text = dict[arrays[num]].ToString();
lvi.ImageIndex = 0;
lvi.Tag = dict[arrays[num]].ToString();
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = arrays[num];
lvi.SubItems.Add(lvsi);
list.Items.Add(lvi);
list.EndUpdate();
num++;
}
#endregion
}
Good luck!
int J = 0;
In this simple code we will remove duplicates from a loop which will take the first item and compare it with the items bellow, so if it exists you can remove the above one or the second one
foreach (ListViewItem item1 in listView1.Items)
{
//J+1 to do not repeat the list from first and remove itself just take a look on next items
for (int i=J+1;i<listView1.Items.Count-1;i++)
{
//i compare two subitems that must be unique in my list
if (listView1.Items[J].SubItems[1].ToString() == listView1.Items[i].SubItems[1].ToString())
// listView1.Items.RemoveAt(i); // remove the second one
listView1.Items.RemoveAt(J); // remove the first one and keep the second
}
J++;
}

Categories