Select items from a listbox through a loop? - c#

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

Related

Programmatically set checked item in CheckListBox

I'm trying to set programmatically checked item in checklistbox according to some criteria. This is code:
int chItm = 0;
foreach (DataRowView row in chLBDatumi.Items)
{
if (row["DatumGO"].ToString().Equals(myListItems[chItm].ToString()))
{
chLBDatumi.SetItemChecked(chItm, true);
}
chItm++;
}
But it throws Exception:
List that this enumerator is bound to has been modified. An enumerator
can only be used if the list does not change.
Is there any other way to accomplish that?
Use a for loop instead of foreach:
for (int i = 0; i < chLBDatumi.Items.Count(); i++)
{
if (chLBDatumi.Items[i]["DatumGO"].ToString().Equals(myListItems[chItm].ToString()))
{
chLBDatumi.SetItemChecked(chItm, true);
}
chItm++;
}

Index was outside the bounds of the array in CheckedListBox

I am getting an index error on my else if statement but I'm unable to find the reason for it.
What I am doing is going through a CheckedListBox, if no values are checked print an error else show the selected values in a MessageBox.
Can anybody help me? Thank you!
for (int i = 0; i < checkedListBox1.Items.Count; i++)
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
else if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.CheckedItems[i].ToString());
}
Move the Count-ckeck before the loop:
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
But the important part is that you are looping all items. Then you check for every item if it is checked with GetItemChecked. That's fine, but then you use checkedListBox1.CheckedItems[i] which doesn't contain all items but only the checked items. That's why you get the Index was outside the bounds error.
Instead you just need to use that collection instead of looping all:
for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
MessageBox.Show(checkedListBox1.CheckedItems[i].ToString());
}
You should change
i < checkedListBox1.Items.Count;
To:
i < checkedListBox1.CheckedItems.Count;
checkedListBox1.CheckedItems[i] is the problem. You loop through all the items, but indexing CheckedItems. So when you have 10 items and checked 2nd item and 8th item, CheckedItems will have only two items but you'll be accessing CheckedItems[7] that's why you get the exception.
Use CheckedItems collection to access checked items directly.
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
foreach (var checkedItem in checkedListBox1.CheckedItems)
{
MessageBox.Show(checkedItem.ToString());
}
Why are you checking for CheckedItems.Count inside the for cycle?
Take the first part of the If clause outside of the For cycle.
At the end your code can look like that:
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items[i].ToString());
}

Set checked items in checkedlistbox from list or dataset

I have a CheckedListBox and I would like to check all the items that are in another List.
This code does not work since the CheckedItems property is read-only and the types do not match, but it gives the best idea of what I want to do.
checkedListBox1.DataSource = DataSetSelectAll().Tables[0];
checkedListBox1.ValueMember = "id_table";
checkedListBox1.DisplayMember = "name";
List<tableClass> list = MyCheckedList();
checkedListBox1.CheckedItems = list;
I know this is wrong but do not know how to explain it better.
Its not possible to set(check) many items at a time like this, checkedListBox1.CheckedItems = list;
better you can use for loop like:
List<tableClass> list = MyCheckedList();
for (int count = 0; count < checkedListBox1.Items.Count; count++)
{
if (list.Contains(checkedListBox1.Items[count].ToString()))
{
checkedListBox1.SetItemChecked(count, true);
}
}
andy's answer is right but I have an easier solution. My solution works in windows application.
DataTable dt = MyCheckedList();
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (dr["valueMember"].ToString() == ((DataRowView)checkedListBox1.Items[i])[0].ToString())
{
checkedListBox1.SetItemChecked(i, true);
}
}
}
Note: dt must fill with a dataTable which has all checkedList Values.
I'm not sure why, but I SetItemChecked(index, tf) wasn't giving me what I wanted. This is how I solved it - explicitly setting the CheckedState.
for (int i = 0; i < myCheckedListBox.Items.Count; i++)
{
if (boolList[i])
{
myCheckedListBox.SetItemCheckState(i, CheckState.Checked);
} else
{
myCheckedListBox.SetItemCheckState(i, CheckState.Unchecked);
}
}

add items to ListBox Control from ListView Control

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

get index of item in listbox c#

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.

Categories

Resources