So basically I am using a FOR loop to add and remove options from a listbox.
It functions correctly when selected 1 option (from either remove or select) and it functions correctly when I select two separate options (For example, item[0] and item[4]).
However, I am having trouble when I try to select all options or when I select two items that are side by side ([2],[3].. etc)
Here is my loop for the select function:
protected void btnSelect_Click(object sender, EventArgs e)
{
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
}
}
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
}
The error is basically taking the item and putting it into the "selected" listbox but it is leaving behind one of the two options in the original "snacks" box.
Any ideas?
The problem is, when you remove an item, all of the other items shift downwards - which means the next loop iteration (since it increments your index) "skips" one item.
There are various ways to handle this. The simplest is to just loop backwards:
for (int intCounter = lbSnacks.Items.Count-1; intCounter >= 0; intCounter--)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
This way, when the items "shift", it doesn't matter, since you've already dealt with those items.
Related
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
I have two list boxes and an ASP button. I am trying to use the button to move only the first item in the index of one list box to the other. Unfortunately whenever the button is pushed, it either moves all of the items in the list box or moves the first one in the index as many times as there are items (for example: if there are five items in the list box, it will move the first item to the next list box 5 times. so i have no items in the first list box and five of the first item in the next list box.) Also, the list boxes are in an Ajax update panel (I don't know if that is relevant). Here is the code:
protected void btnMoveFirst_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListBox2.Items.Add(ListBox1.Items[0]);
}
for (int i = ListBox1.Items.Count - 1; i >= 0; i--) {
ListBox1.Items.Remove(ListBox1.Items[0]);
}
}
Replace your code with this code
protected void btnMoveFirst_Click(object sender, EventArgs e)
{
//Add items only once
ListBox2.Items.Add(ListBox1.Items[0]);
ListBox1.Items.Remove(ListBox1.Items[0]);
}
In your code you are adding item till the count of items in ListBox1
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListBox2.Items.Add(ListBox1.Items[0]);
}
That's why it is adding your items more number of times
So here is the code I have the allows me to delete selected items in my listbox.
ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOutput);
selectedItems = lstOutput.SelectedItems;
if (lstOutput.SelectedIndex != -1)
{
for (int i = selectedItems.Count - 1; i >= 0; i--)
lstOutput.Items.Remove(selectedItems[i]);
}
else
MessageBox.Show("Debe seleccionar un email");
The problem is that I have labels at the top that show what the output is. I also have statistics at the bottom of the page. The way the code is now, I am able to delete those, which I don't want. I am unsure of how to prevent these from being deleted.
first of all the fire line is unneeded and you can merger the two first line to:
ListBox.SelectedObjectCollection selectedItems = lstOutput.SelectedItems;
now for the remove of the item that you want to keep. you can make a condition.
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(selectedItems[i] is /*here is where you come and check if the current selected item is of the type you don't want to delete*/)
lstOutput.Items.Remove(selectedItems[i]);
}
if you'll tell me what's the type of the "labels at the top" and "statistics at the bottom" i'll put it in the answer
EDIT
in similar to what you said you can do:
List<object> fixedItems = new List<object>();
fixedItems.Add(/*Your labels and statistics*/);
and then do
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(fixedItems.Contains(selectedItems[i]) == false)
lstOutput.Items.Remove(selectedItems[i]);
}
for the list you'll need to add
using System.Collections.Generic;
at the beginning of the page
I have 14 Dropdownlist accroding to 7 Days.
Like For a day First dropdown list is Named as From-Time and Second Dropdown list is To-Time.
List Values are Set by 30 minutes Time Difference.
To-Time dropdownlist Should save only those list items which fall after From-time Dropdownlist.like if i select 1 Pm by first then second dropdown list should carry list-items after 1 Pm.
removing is done like this..
protected void ddlMonst_SelectedIndexChanged(object sender, EventArgs e)
{
RemoveListItem(sender as DropDownList,checkboxes);
}
private void RemoveListItem(DropDownList DDl,DropDownList[] checkboxes)
{
int CurrrentSelectedIndex = DDl.SelectedIndex;
String StartDDlName = DDl.ID.Substring(3, 3).ToString() + "ed";
String TargetedDDlName = string.Empty;
for (int i = 0; i < checkboxes.Length; i++)
{
TargetedDDlName = checkboxes[i].ID.Substring(3, 5).ToString() ;
if (StartDDlName.Equals(TargetedDDlName))
{
for(int j=0 ;j<CurrrentSelectedIndex;j++)
checkboxes[i].Items.RemoveAt(0);
}
}
}
but this logic fails if i selected again and again from First Dropdown list.It reomoves all from second one.
How to Handle this situation
Assuming your code is successfully removing the Items, then it makes sense that the To-Time DropDowns could end up being empty after multiple successive selects from the From-Time DropDown(s).
Instead of using .RemoveAt(), you could .Clear() your To-Time DropDowns and then .Add() the Items from the From-Time DropDown from its SelectedIndex and onwards.
// ...
// get reference to To/From DropDown(s) here
// ...
dd_time_to.Items.Clear();
for (int i = dd_time_from.SelectedIndex; i < dd_time_from.Items.Count; i++)
{
dd_time_to.Items.Add(dd_time_from.Items[i]);
}
You'll have to modify this to work on your DropDownList[]
I have a listview with two columns and I'm using a context menu to allow users to remove selected rows. To remove the selected rows, I've tried with the following code but it doesn't work:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
listView1.SelectedItems.Clear();
}
I suspect this is because the listview has two columns, but I can't figure out a solution to remove selected rows. Removing all rows works with: listView1.Items.Clear();.
The latest example of BeefTurkey looks correct, but he should decrement the variable i after removing a selected item:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
The index of items larger as i is decremented by 1 after the removal. So you should reposition i to match the next not tested item.
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
foreach(ListViewItem lvItem in lvDocument.SelectedItems)
{
lvDocument.Items.Remove(lvItem);
}
This seems to work:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].SubItems.Clear();
}
}
Is there any way to remove items and re-order the listView so that there are no empty rows in the middle of other rows?
This seems to be a better solution:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
}
}
What you can do:
foreach (ListViewItem Item in LstvClients.Items)
{
if (item.Selected)
{
LstvClients.Items.Remove(Item);
}
}
(Yours is better, item.Remove())
I have been using something slightly different then the others to remove all the selected items from a ListView control:
foreach (ListViewItem listViewItem in listView1.SelectedItems)
{
listView1.Items.Remove(listViewItem);
}
I'm not sure how this would match up performance-wise to the other posted methods on large lists, but I think it is a little cleaner looking in cases where that isn't an issue.
This is the correct way to remove all selected items. The method is to always access fist selected item with an index 0 and loop until no more selected items left. You cannot refer to other items inside collection with an absolute index safely since indexes will change as soon as you delete one of the items.
while( listView1.SelectedItems.Count > 0)
{
listView1.Items.Remove(lvFiles.SelectedItems[0]);
}
do
{
this.listView1.CheckedItems[0].Remove();
} while (this.listView1.CheckedItems.Count > 0);
This works better