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
}
Having a problem trying to check all the items in a group except the first one, I'm using the following code below, what it actually does is just check all the items in the listview except the first item.
foreach (ListViewGroup grp in listFiles.Groups)
{
foreach (ListViewItem item in grp.Items)
{
if (item.Index != 0)
{
item.Checked = true;
}
}
}
Sorry if the question is a bit confusing:
here is what it does:
What i want it to do:
Thanks for any input.
Try this:
int i;
foreach (ListViewGroup grp in listFiles.Groups)
{
i = 0;
foreach (ListViewItem item in grp.Items)
{
if (i != 0)
item.Checked = true;
i++;
}
}
Dont use Index property,
foreach (ListViewGroup grp in listFiles.Groups)
{
bool FirstItem = false;
foreach (ListViewItem item in grp.Items)
{
if (!FirstItem)
{
item.Checked = false;
FirstItem = true;
}
else
{
item.Checked = true;
}
}
}
foreach (ListViewGroup grp in listFiles.Groups)
{
int i = 0;
foreach (ListViewItem item in grp.Items)
{
if (i != 0)
{
item.Checked = true;
}
i++;
}
}
The question is already answered, but here is another alternative:
foreach (ListViewGroup grp in listFiles.Groups)
{
foreach (var item in grp.Items.Cast<ListViewItem>().Skip(1))
{
item.Checked = true;
}
}
I'm trying to show the selected items of listBox1 in a Message Box here's the code:
int index;
string item;
foreach (int i in listBox1 .SelectedIndices )
{
index = listBox1.SelectedIndex;
item = listBox1.Items[index].ToString ();
groupids = item;
MessageBox.Show(groupids);
}
The problem is that when I select more than one item
the message box shows the frist one I've selected and repeats the message
EX: if I selected 3 items the message will appear 3 times with the first item
You can iterate through your items like so:
foreach (var item in listBox1.SelectedItems)
{
MessageBox.Show(item.ToString());
}
The i in the foreach loop has the index you need. You're using listBox1.SelectedIndex which only has the first one. So item should be:
item = listBox1.Items[i].ToString ();
How about 1 message box with all the selected items?
List<string> selectedList = new List<string>();
foreach (var item in listBox1.SelectedItems) {
selectedList.Add(item.ToString());
}
if (selectedList.Count() == 0) { return; }
MessageBox.Show("Selected Items: " + Environment.NewLine +
string.Join(Environment.NewLine, selectedList));
If any are selected, this should give you a line for each selected item in your message box. There's probably a prettier way to do this with linq but you didn't specify .NET version.
Try this solution:
string item = "";
foreach (int i in listBox1.SelectedIndices )
{
item += listBox1.Items[i] + Environment.NewLine;
}
MessageBox.Show(item);
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.
in the next example how can I know the current row index?
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
}
You have to create one yourself
var i = 0;
foreach (DataRow temprow in temptable.Rows)
{
this.text = i;
// etc
i++;
}
or you can just do a for loop instead.
I have a type in MiscUtil which can help with this - SmartEnumerable. It's a dumb name, but it works :) See the usage page for details, and if you're using C# 3 you can make it even simpler:
foreach (var item in temptable.Rows.AsSmartEnumerable())
{
int index = item.Index;
DataRow value = item.Value;
bool isFirst = item.IsFirst;
bool isLast = item.IsLast;
}
If you can use Linq, you can do it this way:
foreach (var pair in temptable.Rows.Cast<DataRow>()
.Select((r, i) => new {Row = r, Index = i}))
{
int index = pair.Index;
DataRow row = pair.Row;
}
You actually Don't. One of the beauties with foreach is that you don't have the extra set of code handling incrementing and checks on the length.
If you want to have your own Index you would have to do something like this
int rowindex = 0;
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
this.text = rowindex++;
}
int rowIndex = temptable.Rows.IndexOf(temprow);
It's not possible with a standard foreach loop. The simplest way is to use a for loop
for ( int i = 0; i < temptable.Rows.Count; i++ ) {
DataRow temprow = (DataRow)temptable.Rows[i];
...
}
Another option is to use an extension method
public static void ForEachIndex<T>(this IEnumerable<T> e, Action<T,int> del) {
var i = 0;
foreach ( var cur in e ) {
del(cur,i);
}
}
...
temptable.Rows.Cast<DataRow>.ForEachIndex((cur,index)
{
...
});
Hey there's a much faster way I think. No iteration required!
First, declare a static variable for the Friend RowID Field of the DataRow:
Private Shared RowIDFieldInfo As System.Reflection.FieldInfo = GetType(DataRow).GetField("_rowID", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
Then All you need to do to use it is:
RowIDFieldInfo.GetValue(MyDataRow) - 1
I have not tested this after resorting or filtering.
In my case I haven't a need to do that, so this works.
Better late than never...
foreach (DataRow temprow in temptable.Rows)
{
temptable.Rows.IndexOf(temprow);
}
Write any Cell number and get RowIndex
foreach (var item in datagridview.Rows)
{
//TextBox1.Text= item.Cells[0].RowIndex.ToString();
}
Either use a for-loop, or use an integer follow along:
int count =0;
foreach (DataRow temprow in temptable.Rows)
{
//count is the index of the row in the array temptable.Rows
//this.text = temprow.INDEX????
++count;
}
You can use the standard for loop to get the index
for(int i=0; i<temptable.Rows.Count; i++)
{
var index = i;
var row = temptable.Rows[i];
}
While LFSR's answer is right, I'm pretty sure calling .IndexOf on just about any collection/list is going to enumerate the list until it finds a the matching row. For large DataTable's this could be slow.
It might be better to for (i = 0; i < temptable.Rows.Count; i++) { ... } over the table. That way you have the index without imposing a find-the-index tax.
The alternative way to retrieve data by using index instead of using column name
foreach (DataRow temprow in temptable.Rows)
{
String col1 = temprow[0].ToString().Trim();
String col2 = temprow[1].ToString().Trim();
}
Hope it help