In my project I am trying to add the SelectedItems of ListView control (Only Items not sub items) to the ListBox Control. After adding, the selected Items of the ListView Control should get deleted. I am very close but I think I am doing something wrong in my code which leaving single selected item in the ListView control.
ListView --> lvEmpDetails
ListBox --> lbxEmpName
I tried the below code:
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
lvEmpDetails.SelectedItems[intCount].Remove();
}
Please suggest.
Don't delete the items in the same loop you're iterating them. Add them to a list and delete them afterwards:
var itemsToRemove = new List<ListViewItem>();
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
itemsToRemove.Add(lvEmpDetails.SelectedItems[intCount]);
}
foreach (var item in itemsToRemove)
{
item.Remove();
}
You may do another way:
for (int intCount = 0; intCount < lvEmpDetails.SelectedItems.Count; intCount++)
{
lbxEmpName.Items.Add(lvEmpDetails.SelectedItems[intCount].Text);
lvEmpDetails.SelectedItems[intCount].Remove();
//Every time remove item, reduce the index
intCount--;
}
Related
I have a SharePoint custom list which has a column Status (CheckBox). I want to count the total number of list items that are checked and need to show it in a label.
You can get the checkbox control value as
SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue(item["MultiChoice"].ToString());
And iterate through the values as
for (int i = 0; i < choices.Count; i++)
{
Console.WriteLine(choices[i]);
}
Cycle through the controls and check if it is a checkbox. If it is then check if it is checked. If it is then add to the count. Display the count at the end.
int count = 0;
foreach (var control in this.Controls)
{
if (control is CheckBox)
{
if (((CheckBox)control).Checked)
{
count++;
}
}
}
MessageBox.Show("Count: " + count);
You can replace the MessageBox with a label.
I have a ListBox in WPF with a DataBinded Xml Collection. I set the SelectionMode to Extended so the user can select multiple items. I have a RemoveItem command which iterates through the selecteditems and removes them from the list:
var selecteditems = this.SelectedItems;
for(int i = 0; i < selecteditems.Count; i++ )
{
ItemBox ouritem = (ItemBox)this.ItemContainerGenerator.ContainerFromItem(this.SelectedItems[i]);
XmlDataProvider prov = this.DataContext as XmlDataProvider;
XmlNode MainNode = prov.Document.SelectSingleNode("//MainNode");
MainNode.RemoveChild(selecteditems[i] as XmlNode);
}
The problem is that after the first item of a selection is deleted, the selection is cleared and the last item of the list is selected.
How can I keep the selection that I started with and make sure all the items are deleted?
How about the old 'take a copy first' approach?:
IList selectedItems = new List<YourDataType>();
foreach (YourDataType item in this.SelectedItems) selectedItems.Add(item);
for (int index = selectedItems.Count - 1; index >= 0; index--)
{
// remove each selected item here
}
Execute your loop in reverse iteration.
var selecteditems = this.SelectedItems;
for(int i = selecteditems.Count-1; i>=0; i-- )
{
ItemBox ouritem = (ItemBox)this.ItemContainerGenerator.ContainerFromItem(this.SelectedItems[i]);
XmlDataProvider prov = this.DataContext as XmlDataProvider;
XmlNode MainNode = prov.Document.SelectSingleNode("//MainNode");
MainNode.RemoveChild(selecteditems[i] as XmlNode);
}
I want to make it so that through a loop, it will select an item from the listbox. I was thinking about doing a for loop. This is (basically) what I want to accomplish:
for (int i = 0; i < lbRooms.Items.Count; i++)
{
lbRooms.Items.Select(i);
// do stuff here with the selected item
}
I know thats not how it works, but I want it to do it like that. I appreciate all the help, thanks =D
EDIT: I think this will work, but I'm sure it can be improved:
for (int i = 0; i < lbRooms.Items.Count; i++)
{
lbRooms.SetSelected(i, true);
}
Try:
lbRooms.setSelected(i, true);
instead of:
lbRooms.Items.Select(i);
You can't select your items like that, You can use indexer to get your item:
for (int i = 0; i < lbRooms.Items.Count; i++)
{
var currentItem = lbRooms.Items[i];
}
If you want to select that item you can set Selected property to true:
currentItem.Selected = true;
foreach (listitem item in lbRooms.Items)
//do item manipulation here
I am trying to get index of each items in a listbox
Here is my code
foreach (ListItem lstItem in lstCostCenter.Items)
{
int x = lstCostCenter.Items.IndexOf(lstItem);
}
But each time its returning 0 only.
Please some one help me.
Thanks
Gulrej
Why not use a for loop?
for (int i = 0; i < lstCostCenter.Items.Count; i++)
{
ListItem lstItem = lstCostCenter.Items[i];
}
The index of the current item is i.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
}
}
This function simply deletes the selected item in listview.. but i want to cut it and paste it somewhere else.
It sounds like you want to remove the selected listitems and move them to another listview.
ListView sourceListView = new ListView();
ListView destListView = new ListView();
var selected = sourceListView.Items
.Cast<ListViewItem>()
.Where(x => x.Selected)
.ToList();
foreach (var item in selected)
{
sourceListView.Items.Remove(item);
destListView.Items.Add(item);
}