get index of item in listbox c# - 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.

Related

Foreach backwards for listboxes?

foreach (ListViewItem lvi in listView1.SelectedItems)
{
lvi.index;
}
This foreaches for all selected items, but how do i do this backwards?
Like first index 3 then 2 then 1.
Thankyou!
foreach (ListViewItem lvi in istView1.SelectedItems.Cast<ListViewItem>().AsEnumerable().Reverse());
You need to use for to iterate over the available indexes
for (int i = (listView1.Items.Count - 1); i == 0; i--)
{
// i is the index
}

Why my "foreach" C# code doesn't loop through all items in listBox?

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
}

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

Select items from a listbox through a loop?

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

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

Categories

Resources