Removing more than 1 item in listbox - c#

please help me how can remove more than 1 item in a listbox.I know the code for removing 1 item: listbox.Items.RemoveAt(i)
but for more than 1 item selected in a listbox,what is the code? write the code for example in button-click event.I have only a button and a listbox in my winform.(write codes in C#)

while(listbox.SelectedItems.Count > 0)
{
listbox.Items.Remove(listbox.SelectedItem);
}

ListBox1.ClearSelection();
//or
foreach (ListItem listItem in ListBox1.Items)
{
listItem.Selected = false;
}
List<ListItem> itemsToRemove = new List<ListItem>();
foreach (ListItem listItem in ListBox1.Items)
{
if (listItem.Selected)
itemsToRemove.Add(listItem);
}
foreach (ListItem listItem in itemsToRemove)
{
ListBox1.Items.Remove(listItem);
}

Related

Dropdownlist not highlighting the given value

i have a dropdown list which i want to highlight a item from it. i have given the condition correct as par to me.But it didnt highlight the given item,instead showing normally as other items.
DataTable dtt = new DataTable();
dtt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dtt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
ddlCompanyName.DataBind();
ddlCompanyName.Items.Insert(0, new ListItem("--Select Name--"));
Compidd(string) has specified item to be highlighted in dropdownlist
You need to do DataBind before the loop:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
EDIT:
To set the value as default value you can try like this
ddlCompanyName.SelectedValue = "The value which you want to set as default"
The ddlCompanyName.DataBind(); must be executed before you loop the items:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
Otherwise there are no items in the DropDownList.

How to skip listitem with index 0 foreach checkboxlist item

I have a CheckBoxList which I am going through all its items using:
foreach (ListItem item in this.checklist.Items)
I was wondering how I can skip the first item in this CheckBoxList(item with index 0).
Linq should help you do the trick. Make sure to cast the items collection to typed collection, and then you can use Skip to skip first item:
foreach (ListItem item in this.checklist.Items.Cast<ListItem>().Skip(1))
I believe one way to do this is:
bool isFirst = true;
foreach (ListItem item in this.checklist.Items)
{
if (isFirst) {
isFirst = false;
} else {
// do checking
}
}
Try this.
foreach (ListItem item in this.checklist.Items)
{
if (item != this.checklist.Items[0]) {
// Do something
}
}

loop through checkedListBox items without the select all item

I want to get the items from a checkedListBox into a List<>, but without the select all/ deselect all (first checkbox)..can't figure it out how to not add the first item..
this is the code:
foreach (string s in checkedListBoxDepts.CheckedItems)
{
if (checkedListBoxDepts.SelectedItems.IndexOf(s) == 0)
continue;
list.Add(s);
}
then I take the items and put in another list to avoid errors:
foreach (string s in list)
{
list2.Add(s);
}
but still the select all is loaded...help
Try:
foreach (var s in checkedListBoxDepts.CheckedItems)
{
if (checkedListBoxDepts.IndexOf(s) == 0)
continue;
list.Add(s.ToString());
}
foreach (string s in checkedListBoxDepts.CheckedItems)
{
if (checkedListBoxDepts.SelectedItems.IndexOf(s) == 0)
continue;
list.Add(s);
}
after that remove first item from list
list.removeat(0);

Unable to transfer Selected Items of a ListBox to another ListBox

I am unable to transfer the Selected Items from one ListBox to another ListBox:
protected void Button2_Click(object sender, EventArgs e)
{
foreach (ListItem li in ListBox2.Items)
{
if (li.Selected)
{
ListItem liNew = new ListItem(li.Text, li.Value);
ListBox1.Items.Add(liNew);
ListBox2.Items.Remove(liNew);
}
}
}
I am getting the exception:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
The problem is that you can't remove elements from a collection while you're iterating it. Instead, you can select the items that are selected :) and loop over them.
foreach(ListItem li in ListBox2.Items.Where(x => x.Selected)) {
ListItem liNew = new ListItem(li.Text, li.Value);
ListBox1.Items.Add(liNew);
ListBox2.Items.Remove(li);
}
(Also, I think you meant li, not liNew.)
Without LINQ, it might look like:
List<ListItem> toRemove = new List<ListItem>();
foreach(ListItem li in ListBox2.Items) {
if(li.Selected) {
ListItem liNew = new ListItem(li.Text, li.Value);
ListBox1.Items.Add(liNew);
toRemove.Add(li);
}
}
foreach(ListItem li in toRemove) {
ListBox2.Items.Remove(li);
}
Also, you can use a for loop, as suggested by #Steve:
for(int i = ListBox2.Items.Count; --i >= 0;) {
ListItem li = ListBox2.Items[i];
if(li.Selected) {
ListItem liNew = new ListItem(li.Text, li.Value);
ListBox1.Items.Add(liNew);
ListBox2.Items.RemoveAt(i);
}
}

C# CheckBox List Selected Items.Text to Labels.Text

I have a CheckBoxList and 5 labels.
I would like the text value of these Labels to be set to the 5 selections made from the CheckBoxList after the user clicks on a button. How would I get this accomplished?
Thanks in advance.
bind an event to a button,
iterate trough the Items property of the CheckBoxList
set the text value according to the selected property of the listitem
like:
protected void button_Click(object sender, EventArgs e)
{
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? "Checked" : "UnChecked";
}
}
to add a value you could do:
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? item.Value : "";
}
or display al values in a mini-report:
string test = "you've selected :";
foreach (ListItem item in theCheckBoxList.Items)
{
test += item.Selected ? item.Value + ", " : "";
}
labelResult.Text = test;
find selected items from CheckboxList by Lambda Linq:
var x = chkList.Items.Cast<ListItem>().Where(i => i.Selected);
if (x!=null && x.Count()>0)
{
List<ListItem> lstSelectedItems = x.ToList();
//... Other ...
}
Why don't you have one label and on the button click do something like:
foreach (var li in CheckList1.Items)
{
if(li.Checked)
Label1.Text = li.Value + "<br />";
}
That may not be the exact syntax but something along those lines.
Use this in LINQ:
foreach (var cbx3 in CheckBoxList2.Controls.OfType<CheckBox>().Where(cbx3 => cbx3.ID == s))
{
cbx3.Checked = true;
}

Categories