i need to remove all unchecked item from listview winforms c# in textchange event
for e.g i need to do like below
private void textBox_supplierName_TextChanged(object sender, EventArgs e)
{
if (listView_supplierNames.CheckedItems==CheckState.Unchecked)
{
// remove item
}
}
how to do it ...thanks
Use ListViewItem.Remove method to remove item from its associated ListView control:
foreach (ListViewItem item in listView_supplierNames.Items)
if (!item.Checked)
item.Remove();
Loop through the ListView Items and Use ListViewItem.Remove to Remove Items
foreach (ListViewItem item in listView_supplierNames.Items)
{
if (item.Checked)
{
}
else
{
//Remove unchecked Items
listView1.Items.Remove(item);
}
}
Get all the unchecked items and use Remove - example-
foreach(var item in listView.SelectedItems)
{
listView.Items.Remove(item)
}
Related
I am trying to remove a specific item on my list (people) when button (remove) is clicked. Instead, only the display on the listview (listName) is removed. When I create another name, the previous names that I had removed are displayed on the lisview again.
public void remove(object sender, EventArgs e)
{
foreach (ListViewItem eachItem in listName.SelectedItems)
{
listName.Items.Remove(eachItem);
}
}
You should make a list of items to remove while enumerating, then use that list to actually remove them, to avoid modifying the collection while enumerating (since modifying Items is really also modifying SelectedItems which you are currently enumerating):
public void remove(object sender, EventArgs e)
{
var itemsToDelete = new List<ListViewItem>();
foreach (ListViewItem eachItem in listName.SelectedItems)
{
itemsToDelete.Add(eachItem);
}
foreach (ListViewItem eachItem in itemsToDelete)
{
listName.Items.Remove(eachItem);
}
}
I am trying to display items in a listview from groups and here is what I mean.
I added a listview to a form and in the listview I added a 2 groups then I added items and for the items I chose a group name.
Now in a combox box I add in the selectindexchanged event I put this.
if (comboBox1.Text == "group1")
{
foreach (string itemname1 in listimages.Groups[0].Items)
{
string currentitem = itemname1;
}
}
nothing is working so I am trying to figure out what I am not doing right.
The items in the combobox have the same items as group names.
Any help would be great.
I was able to figure it out. Here is what I did
First on the listview I added Groups, then I added items and on the items in the Tag property I put in the group I wanted it to be linked to.
In the comboBox I put in group names as I typed them when I added them to the listview.
Then I added this code :
private void frmImageSelection_Load(object sender, EventArgs e)
{
items = new ListViewItem[listimages.Items.Count];
listimages.Items.CopyTo(items, 0);
ShowGroup(0);
cmbgroups.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowGroup(cmbgroups.SelectedIndex);
}
void ShowGroup(int index)
{
if (index == 0) // all
{
listimages.Items.Clear();
listimages.Items.AddRange(items);
}
else
{
listimages.Items.Clear();
foreach (ListViewItem item in items)
if (listimages.Groups[index].Name.Equals(item.Tag))
listimages.Items.Add(item);
}
foreach (ListViewItem item in listimages.Items)
item.Group = listimages.Groups[index];
}
ListViewItem[] items;
Anyway if you are not sure I will be glad to help you out just drop me a message in my inbox or something.
I have a button which I am using to add value from list 1 to list 2
protected void btn_Add_Click(object sender, EventArgs e)
{
lst_grpmembers.Items.Add(lst_allmembers.SelectedItem.Text);
lst_allmembers.Items.Remove(lst_allmembers.SelectedItem.Value);
}
if i click the add button without selecting any value in the list box then I get this error
Object reference not set to an instance of an object.
so i thought I should Add some loop and added this
foreach (ListItem lis in lst_allmembers.Items)
{
if (lis.Selected)
{
lst_grpmembers.Items.Add(lst_allmembers.SelectedItem.Text);
lst_allmembers.Items.Remove(lst_allmembers.SelectedItem.Value);
}
}
for this code I get this error
Collection was modified; enumeration operation may not execute.
How should this adding and removing should be done in two list box. Thanks
protected void btn_Add_Click(object sender, EventArgs e)
{
if( lst_allmembers.SelectedItem != null )
{
lst_grpmembers.Items.Add(lst_allmembers.SelectedItem.Text);
lst_allmembers.Items.Remove(lst_allmembers.SelectedItem.Value);
}
}
Of course, creating a list of selected items handles multi-select:
List<ListItem> listSelected = new List<ListItem>();
foreach( ListItem li in lst_allmembers.Items )
{
if( li.Selected )
listSelected.Add(li);
}
foreach( ListItem li in listSelected )
{
lst_grpmembers.Items.Add(li.Text, li.Value);
lst_allmembers.Items.Remove(li);
}
i solved a similar problem with a temporary ListBox. Take a look at. I hope it helps.
ListBox _temp = new ListBox();
foreach (ListItem item in ListBox1.Items)
if (item.Selected)
_temp.Items.Add(new ListItem(item.Text, item.Value));
foreach (ListItem item in _temp.Items)
ListBox1.Items.Remove(item);
I have a Listener on the DoubleClick event in a ListView. I have also activated FullRowSelect.
So when I double-click a row, only the value in the first column appears. I also tried it directly with SelectedItems.
Please help
Code:
private void lvRecipesPos_DoubleClick(object sender, EventArgs e)
{
String s = "";
foreach (ListViewItem item in lvRecipesPos.Items)
{
if (item.Selected == true)
{
s += item.Text.ToString();
}
}
MessageBox.Show(s);
}
1) The ListView has a SelectedItems collection, so you don't have to iterate all items and check if they're selected.
2) Item has a SubItems collection which holds the texts for all sub items
I want to delete one or more selected Items from a ListView.
What is the best way to do that?
I´m using C# and the dotnet Framework 4.
You can delete all selected items by iterating the ListView.SelectedItems collection and calling ListView.Remove for each item whenever the user pressed the delete key.
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Delete == e.KeyCode)
{
foreach (ListViewItem listViewItem in ((ListView)sender).SelectedItems)
{
listViewItem.Remove();
}
}
}
I think there is something called listView.Items.Remove(listView.SelectedItem) and you can call it from your delete button's click event. Or run a foreach loop and see if the item is selected, remove it.
foreach(var v in listView.SelectedItems)
{
listView.Items.Remove(v)
}
I think this is the easiest mode.
private void listView_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
this.listView.Items.Remove(listView.SelectedItem);
}
}
Try this:
// Get an array of all selected items
ListViewItem[] selectedItems = (from i in listView.Items where i.Selected select i).ToArray();
// Delete the items
foreach (ListViewItem item in selectedItems)
listView.Items.Remove(item);
EDIT
I just noticed that the ListView class already has a SelectedItems property. To make sure that you're not changing the collection you're iterating on, I'd copy that collection first:
Seems the above (using AddRange) did not work. I thought that removing the items by iterating over the SelectedItems enumerable would cause an exception, but obviously it does not. So my original code code be modified to match the other answers... sorry for posting non-functional code...
I know this is a bit unrelated but In WPF the mentioned methods did not work for me. I had to create a copy of the selected items and use these to remove items in the listview.:
private void ListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Delete)
{
var lst = new List<object>();
foreach (var itemSelected in ListBox.SelectedItems)
{
lst.Add(itemSelected);
}
foreach (var lstitem in lst)
{
ListBox.Items.Remove(lstitem);
}
}
}