c# loop listbox selected items - c#

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!

Related

how to print the checked items from the checked listbox to a label in c#?

I'm Trying to fetch items from checked listbox when the user checks the listbox item it should be displayed in a label on a button click. I tried using this:
foreach (object item in checkedlistbox1.CheckedItems)
{
labelto.Text += checkedlistbox1.SelectedItem.ToString();
}
But I'm getting this exception:
List that this enumerator is bound to has been found, enumerator can only be used if the list does not change.
How to print the checked items from the checked listbox to a label?
This is not a complicated task, why don't you make try with the following:
string displayText = "";
foreach(object item in checkedListBox1.CheckedItems)
{
DataRowView castedItem = item as DataRowView;
displayText += castedItem["boundPropertyNameHere"];
}
labelto.Text = displayText;
Please note:
Where boundPropertyNameHere be the name of property that used to bind the collection.

Clear the Selection of an Item in a ListBox C#

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++;
}

How to get selected items of a checkboxlist using a foreach loop?

I have a checkboxlist which is populated from the database. Number of items in the list can be differed. I want to get all the selected values using a foreach loop and store it in the database. How can i do it?
You can use a foreach loop and test every checkbox list element if it is checked like this:
foreach (ListItem item in youListName.Items)
{
if (item.Selected){
//do something with your item
}
}

C# - Remove items from List when button is clicked

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());
}

How do I find out if individual items in CheckedListBox are checked? C#

I've tried looking in checkedListBox1.Items, but that didn't help. So how do I check if an item in a CheckedListBox is marked? It's in a windows forms application.
You can get a list of checked items using CheckedItems property.
Example 1:
foreach (var item in this.checkedListBox1.CheckedItems)
{
MessageBox.Show(item.ToString());
}
Example 2:
this.checkedListBox1.CheckedItems.Cast<object>()
.ToList()
.ForEach(item =>
{
//do stuff here
//for example
MessageBox.Show(item.ToString());
});
If you are sure items are string for example, you can use Cast<object> in above code.
You can get the list of checked indices using CheckedIndices property.
Example:
this.checkedListBox1.CheckedIndices.Cast<int>()
.ToList()
.ForEach(index =>
{
//do stuff here
//for example
MessageBox.Show(this.checkedListBox1.Items[index].ToString());
});
try this:
foreach (ListItem item in checkedListBox1.Items)
{
if (item.Selected)
{
// If the item is selected
}
else
{
// Item is not selected, do something else.
}
}

Categories