My problem is; When click a button I input items in List, next in DropDownList. Problem is where i click button again exist items again into my DropDown.
How to solve this problem(sorry for image)?
List<string> companyList = new List<string>();
foreach (string item in companyList.ToList())
{
companyList.Remove(item); ----> this not working.......
}
foreach (SPListItem item in myItemCol)
{
companyList.Add(item["Company"].ToString());
}
companyList.Sort();
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}
You can use the Contains method to check if it is already there
if(!ddComFilter.Items.Contains(items.ToString())
{
ddComFilter.Items.Add(item.ToString());
}
This will only add the item if it is not already in the dropdown
You could check for existence of the item before add it to the list.
foreach (SPListItem item in myItemCol)
{
if(!companyList.Contains(item["Company"].ToString())
{
companyList.Add(item["Company"].ToString());
}
}
Then you need to clear the ddComFilter before adding the values to it:
companyList.Sort();
ddComFilter.Items.Clear();
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}
Alternate solution:
You can bind the ddComFilter using the generated list, instead for iterating the collection and add one-by-one. if so you need not to clear the collection, remove items etc. The code for this will be:
ddComFilter.Datasource = companyList;
ddComFilter.DataBind();
Here is an useful article for you
You should clear your dropdown before adding list as items:
companyList.Sort();
ddComFilter.Items.Clear(); // clear
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}
Related
I have a multi-select list box. When an item is selected a document is opened in a tab control. When the document is closed I want to un-select the item in the list box. I don't want it removed from the collection, I don't want to clear all selections. I just want to clear that particular selection.
In the OnRequestClose() method;
string itemName=workTab.DisplayName;
foreach (QResult r in FileListBox.SelectedItems)
{
If(r.FileName = itemName) //Clear the Selection
That is my approach but I can't seem to get the syntax and the examples I find are for clearing all or removing the selected items from the list.
Thanks for the help.
This should work:
foreach (var r in FileListBox.SelectedItems.Cast<QResult>().ToList())
{
if (r.FileName == itemName) //Clear the Selection
{
FileListBox.SelectedItems.Remove(r);
}
}
Have you tried:
string itemName=workTab.DisplayName;
var i=0;
while (i<FileListBox.SelectedItems.Count)
{
QResult r = FileListBox.SelectedItems [i]
if(r.FileName = itemName){
FileListBox.SelectedItems.Remove(r);
}
i++;
}
I have a listbox on a form set to allow multiple selections. I want to loop through each selected item, store the selected value in a variable and do some work. I've tried many different variations of code to do this, but so far nothing has worked. Any help would be greatly appreciated! My code is below:
foreach (var item in systemList.Items)
{
string systemName = systemList.SelectedItems.ToString();
//do some work//
}
You can get all SelectedItems using below code:
var items = systemList.Items.Cast<ListItem>().Where(item => item.Selected);
You can then loop through the items
foreach (var item in items)
{
//Access value of each item by calling item.Value
}
foreach (var item in systemList.SelectedItems)
{
string systemName = item.ToString();
//do some work//
}
make sure listbox Selection mode is set to other than single!
I am trying to remove selected items in a ListBox which is bound to ObservableCollection.
var selectedFiles = MyList.SelectedItems;
foreach (cListEntry item in selectedFiles)
{
_myList.Remove(item);
}
"Collection was modified; enumeration operation may not execute"
What is the proper way of doing this?
You can't modify the collection while enumerating it as evident from the exception itself.
Explanation:
When you remove item from ObservableCollection, MyList.SelectedItems gets update since ObservableCollecton implement INotifyCollectionChanged.
Now, selectedFiles is pointing to same reference which results in modifying it.
Solution
Instead create a new list and enumerate over that so that any change in ObservableCollection doesn't reflect back to list which you are enumerating. This will work:
var selectedFiles = MyList.SelectedItems.Cast<object>().ToList();
foreach (cListEntry item in selectedFiles)
{
_myList.Remove(item);
}
This happens when trying to modify an ObservableCollection<T> that is bound to a ListBox, for example. This is how you deal with that:
ObservableCollection<Employee> itemsToRemove = new ObservableCollection<Employee>();
foreach (Employee item in lsbxNames.SelectedItems)
{
itemsToRemove.Add(item);
}
foreach (Employee item in itemsToRemove)
{
((ObservableCollection<Employee>)lsbxNames.ItemsSource).Remove(item);
}
Create a new ObservableCollection<T> called itemsToRemove, with the same T as your collection you are trying to modify.
Iterate through your nodes of SelectedItems in your ListBox. Add them to itemsToRemove.
Iterate through itemsToRemove. Cast the ListBox ItemsSource to an ObservableCollection<T> and remove the matches in itemsToRemove from it.
Reference: http://docs.telerik.com/devtools/wpf/controls/radgridview/managing-data/deleting-entry
So this would mean you should be able to do this:
ObservableCollection<cListEntry> itemsToRemove = new ObservableCollection<cListEntry>();
foreach (cListEntry item in MyList.SelectedItems)
{
itemsToRemove.Add(item);
}
foreach (cListEntry item in itemsToRemove)
{
((ObservableCollection<cListEntry>)MyList.ItemsSource).Remove(item);
}
I'm not sure what _myList is, but you don't need to modify it. Just go directly to the ListBox.
I want to know if there is a possibility to filter a listbox. I mean it in such a way that is i add an item and the name is already in the listbox that you get a messagebox.show that tells you"Item already in the listbox". And that it won't be added twice.
You don't need to iterate throug the items as the Items collection of the ListBox implements the "Contains" method.
if (listBox1.Items.Contains(Item))
{
MessageBox.Show("ListBox already contains Item");
}
"Item" is in this case the Item from the other ListBox
Update. You could write:
if (listBox1.Items.Contains(listBox2.SelectedItem))
{
MessageBox.Show("ListBox already contains Item");
}
else
{
listBox1.Items.Add(listBox2.SelectedItem);
}
Use data binding might be one of the solutions:
List<string> SomeData=...
var filtered=SomeData.Where(...); // <-- Your filtering condition here
listBox1.DataSource = new BindingSource(choices, null);
Inside the event/method which adds list items inside your listbox you can add something like:
// search for list item in the listbox which has the text
ListItem li = theListBox.Items.FindByText("yourListItemName");
if (li != null)
{
// if list item exists display message
MessageBox.Show("ListBox already contains item with the name");
}
else
{
theListBox.Items.Add("yourListItemName");
}
here is a sample code try and implement it in you code
ListBox.ObjectCollection ListItem1= ListBox1.Items;
if(!string.IsNullOrEmpty(SearchBox.Text))
{
foreach (string str in ListItem1)
{
if (str.Contains(SearchBox.Text))
{
msgbox;
}
}
}
List itemsToMove = new List();
foreach (ListViewItem item in lvScanRepository.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
if (!lvBatch.Items.Contains(item))
{
lvScanRepository.Items.Remove(item);
lvBatch.Items.Add(item);
}
}
A ListViewItem can't belong to more than one ListView at the same time, so this condition:
if (!lvBatch.Items.Contains(item))
... will always be true.
What criteria do you want to use to determine whether the item in one ListView is "similar" to an item in another? Depending on that, you have a couple of options:
ListViewItem has a property called Name which can be used to uniquely identify items in a ListView. You can then call Items.ContainsKey(String) to see if an item exists with that name.
Alternatively you can search in lvBatch to find an item with the same Text as the one you're trying to add:
if (!lvBatch.Items.Cast<ListViewItem>().Any(i => i.Text == item.Text))
(You need to cast because ListViewItemCollection doesn't actually implement IEnumerable<ListViewItem>.)