ListViewItem.Index property gets the zero-based index of the item within the ListView control.
Now I need index of certain item in a ListViewGroup , not the whole control.
Is it possible?
You can find its index within the group as such :
ListViewItem item = /* certain item in a ListviewGroup */;
var index = item.Group.Items.IndexOf(item);
Related
Sorry for the confusing title but here is my situation: When I select an item from a listbox I want a label showing that item's number. For example if I choose the fifth item in the listbox the label should show "Item number 5 is selected".
How do I do this?
This is simple. All you need to do to get the index of the selected item in the listbox is to use the SelectedIndex property which returns the zero-based index of the selected item. If you want a one-based index instead, just add 1 to the index.
int index = listBox.SelectedIndex;
label.Text = $"Item number {index + 1} is selected.";
In my program I have 2 listviews and 1 button.
When I press the button, every listview item in the first listview will be selected in the second listview. The items in the first listview always exist in the second listview, but not the other way around.
I'm having trouble with selecting the items in the second listview. I'm trying to get the index with IndexOf.
foreach (ListViewItem item in firstListView.Items)
{
int index = secondListView.Items.IndexOf(item);
secondListView.Items[index].Selected = true;
}
I always get an error that index is -1 when I click the button. And I don't understand what I'm doing wrong.
SOLUTION
So what I tried to do here finding an index of an item which belongs to a different listview, and it doesn't work like that. Even though the text in both listviews are the same, the listview items are not identically the same because they are reference types.
I was not able to use the Text property because items could have the same text. So the solution I had was putting an unique integer in the Tag property for each ListViewItem. Since integers are value types, I can use that integer to check whether the ListViewItem in the first listview is in the second.
// Loop through each item in the first listview
foreach (ListViewItem item1 in firstListView.Items)
{
// For each item in the first listview, loop through the second listview to find if it's there
foreach (ListViewItem item2 in secondListView.Items)
{
// Check if item1 is identical to item2 by looking at its tag
if (int.Parse(item1.Tag) == int.Parse(item2.Tag))
{
// The item has been found and will be selected!
item2.Selected = true;
}
}
}
You can use such linq query to select items of the second list which exist in the first list too:
var items = from i1 in listView1.Items.Cast<ListViewItem>()
from i2 in listView2.Items.Cast<ListViewItem>()
where i1.SubItems.Cast<ListViewItem.ListViewSubItem>()
.Where((s, i) => s.Text != i2.SubItems[i].Text).Count() == 0
select i2;
items.ToList().ForEach(x => { x.Selected = true; });
Note:
When to try to use secondListView.Items.IndexOf method to find an item which belongs to the firstListView you can not expect it to find the item. The item you are trying to find its index, doesn't exists in second listview's item collection. You should find items using Text property of the item and sub items.
Well the same item obviously cant exist in two different lists.
If the text is the same, then try looking by it:
foreach (ListViewItem listViewItem in l1.Items)
{
var item = l2.Items.Cast<ListViewItem>().Where(lvi => lvi.Text == listViewItem.Text);
item.Selected=true;
}
Or:
foreach (ListViewItem listViewItem in l2.Items.Cast<ListViewItem>()
.Where(lvi => l1.Items.Cast<ListViewItem>().Any(lvi2 => lvi.Text == lvi2.Text))
{
listVieItem.Selected=true;
}
IndexOf returns -1 if it cannot find the object passed to it in the list. My guess is that you have an issue with your equality comparison. Do the objects in the lists implement IComparable? Otherwise they may not be finding the items correctly and are reverting to a bad equality comparison. ListViewItem may not be comparing how you expect.
you can try finding the item by text and then get the index like this
foreach (ListViewItem item in firstListView.Items)
{
var itm = secondListView.FindItemWithText(item.Text);
int index = secondListView.Items.IndexOf(itm);
secondListView.Items[index].Selected = true;
secondListView.Select();
}
what FindItemWithText will do is get the item in secondListView which will be of same name as in firstListView and IndexOf will get the index of item in secondListView
Edit
This solution is good when you have two lists whose items are same and does not include any repetition in name else if you have two items in secondListView with same name FindItemWithText will get the first item only.
I have to force my combobox to change the selected index when user enters text and there is an item match. Right now i am getting the item match from my combobox like this:
List<DataRowView> deliveryRoutes = ((ComboBox)sender).Items.Cast<DataRowView>().ToList();
if (deliveryRoutes.Where(q => q.Row[0].ToString().ToLower().Equals(((ComboBox)sender).Text.ToLower())).Count() != 0)
{
}
This code checks if the user input is a match with the combobox datasource. in my if statement i want to set the selected index of my combobox to be the matched text. Like so:
DeliveryRouteID.SelectedIndex = matchedTextIndex
I have tried getting the index from this without any luck:
deliveryRoutes.Where(q => q.Row[0].ToString().ToLower().Equals(((ComboBox)sender).Text.ToLower())).FirstOrDefault().Row[0]
How would i get the index and set it to be the selected index ?
You're looking for ComboBox.FindStringExact or ComboBox.FindString
cmb.SelectedIndex = cmb.FindStringExact(item);
How can i get the index when adding my item to the ListView?
Im adding the item as follows
flatListView1.Items.Add(name).SubItems.AddRange(row1);
You may need this, as the item added at the end of listitem and will be last. So total items -1 will be index because index starting from zero.
flatListView1.Items.Add(name).SubItems.AddRange(row1);
int index = flatListView1.Items.Count -1;
var lviewItem=flatListView1.Items.Add(name)
var index=flatListView1.Items.IndexOf(lviewItem)
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.indexof.aspx
I want to reach checked rows item in ASP.Net Listview control.
For example:
if 3 Lines of Listview checked, I want to reach checked items value in listview.
How can I do this?
Thanks.
You can use listview property 'CheckedItems'.
You can get list of items which are checked using listview.CheckedItems. For the value in that item, based on the subitem index you need to fetch it. For ex.
ListView.CheckedListViewItemCollection checkedItems =
ListView1.CheckedItems;
foreach ( ListViewItem item in checkedItems )
{
value = item.SubItems[1].Text;
}