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);
}
Related
What is expected: tool to do task on each item, one by one in the listbox.
What is happening: tool does the task only on the user selected item in the listbox. And nothing else.
Code:
int index;
string item;
foreach (int i in listBox1.SelectedIndices)
{
index = listBox1.SelectedIndex;
item = listBox1.Items[index].ToString();
texteditor.Documents.Open(#item);
}
this should iterate through all items
foreach (var item in listBox1.Items)
{
texteditor.Documents.Open(item.ToString());
}
You are iterating over selected items, whereas you want to interate over all the items in your list box.
Also, the correct way to iterate over a ListBox returns a ListItem and not an int.
Try this:
string item;
foreach (ListItem li in listBox1.Items)
{
item = li.ToString();
texteditor.Documents.Open(#item);
}
I also removed the index variable from your code. If you needed the index for something else you would need a for loop like this:
string item;
for (int index = 0; index < listBox1.Items.Count; index++) {
item = listBox1.Items[index].ToString();
texteditor.Documents.Open(#item);
// do something with index
}
In the top of form1:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
List<string> results = new List<string>();
Then:
ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();
for (int i = 0; i < result.Items.Count; i++)
{
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
And in the end:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
What i wanted to do in general is to add to the combobox the titles and then when i select a title to get the title id.
For example i run the program and select the title Weather now i want to see in a messageBow.Show the id 1
There are 31 items.
When i use a breakpoint and look on result i see 31 items when i click on the first item in index 0 i see Id = "1" and then i click on snippet and see title "weather"
Then i do the same for item in index 1 and i see Id = "19" and in the snippet the title is "animals".
But for some reason it's adding each itertion the same item many times.
Create a new instance of ComboboxItem each time you want to add a new item to the combo box:
for (int i = 0; i < result.Items.Count; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
Your code changes the properties of the same item instance for each entry in results, then adds it to the comboBox1.Items collection. Add inserts its argument to the Items collection, it doesn't copy its contents. As a result, when the combobox is rendered, all combobox items point to the same item. To avoid this, create a new item instance for each entry in results:
for (int i = 0; i < result.Items.Count; i++)
{
var item=new ComboboxItem
{
Text = result.Items[i].Snippet.Title,
Value = result.Items[i].Id
};
comboBox1.Items.Add(item);
}
or
var items=from item in result
select new ComboboxItem
{
Text = item.Snippet.Title,
Value = item.Id
};
comboBox1.Items.AddRange(items);
You could do a simple check to make sure that the combobox doesn't already contain it before doing the insert.
ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();
for (int i = 0; i < result.Items.Count - 1; i++)
{
if(!comboBox1.Items.Contains(item))
{
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
}
Or you can do like this article suggests and remove every item that is the same before adding the new item to remove conflicts, No Duplicate in a Listbox or using the same way to stop duplicates from a combobox too
I have a listbox that is bound to a list of objects from the database. I have a secondary list that has less objects that I want to use it to mark as selected elements.
cell = new HtmlTableCell();
List<ClasaAutor> listaAutori = DataTableToClasaAutor(dal.CitesteTotiAutori());
List<ClasaAutor> listaAutoriPublicatie = DataTableToClasaAutor(dal.CitesteTotiAutoriUneiPublicatii(guidPublicatie));
ListBox list = new ListBox();
list.SelectionMode = ListSelectionMode.Multiple;
list.ID = "cbAutori";
list.DataSource = listaAutori;
list.DataTextField = "NumeComplet";
list.DataValueField = "GuidAutor";
list.DataBind();
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Value == autor.GuidAutor.ToString())
list.SelectedIndex = i;
}
}
cell.Controls.Add(list);
row.Cells.Add(cell);
The problem is that only my last element gets selected... why? How can I fix it?
My if is ok, it gets true 2 times...
Try this loop:
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
foreach (ListItem item in list.Items)
{
if (item.Value == autor.GuidAutor.ToString())
item.Selected = true;
}
}
the problem in semantics, SelectedIndex of list can hold only one value, this is not collection
however you could make list item selected by setting the Selected value of it to true
list.Items[i].Selected = list.Items[i].Value == autor.GuidAutor.ToString();
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--;
}
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);
}