Is there a way to get a Listbox Item Index by its content? Something like:
id = listbox.Items.Contains("text");
I know that this way I'll get a bool result, but I don't know how to get the item Index. If I could get the Index I could be able to remove items with
listbox.Items.RemoveAt(id);
In WPF
ListBox listBox = new ListBox();
int index = listBox.Items.IndexOf(item);
Look for the item's index, and then you can remove it with the given method:
int index = myListBox.Items.IndexOf(itemToSearch);
/*if there is no coincidence, it returns -1, so you must check for that return, else RemoveAt(-1) would give you a runtime error.*/
if (index >=0)
{
myListBox.Items.RemoveAt(index);
}
Assuming the item is not selected but for some reason you just want to find it in the list:
The listbox.items is a listbox object collection:
So:
id=listbox.items.IndexOf("text");
will do the trick.
Related
I'm seraching for a way to delete a specific item in a ComboBox by index.
I only found a way to delete the item by value with this code:
cbRooms.Items.Remove((ComboBoxItem)item))
Update
I want to use a code like that
ComboBox.Items.Remove(ComboBox.SelectedIndex);
You can remove item by index, using RemoveAt method:
comboBox.Items.RemoveAt(index);
It is as simple as :
cbRooms.Items.RemoveAt(0); //0 = Index
If you want the get the index of the selected value and remove it, you can do something like :
int SelectedIndex = cbRooms.SelectedIndex;
// OR LIKE : int SelectedIndex = cbRooms.FindString(textBox2.Text);
cbRooms.Items.RemoveAt(SelectedIndex); //0 = Index
Hope that what you are looking for and my answer helped you.
For example if I click on the first item it will be at index 0.
If I click on item 15 then the index should be 16.
I tried
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listView1
}
But I'm not sure if this is the right event or I should use the listView1_Click event ?
And the listView1 does not have any property SelectedIndex.
And last thing is I want to get the item text according to the index of the item I clicked on.
Assuming you want the index of the currently selected item you can do it like this :
int index = ListView1.FocusedItem.Index
Use ListView.SelectedIndices property:
List<int> selectedIndices = listView1.SelectedIndices.Cast<int>().ToList();
It returns collection of selected indices (because by default you can select several items in listview if you click on items with Ctrl or Shift key pressed). Also note that when you deselect all items, this collection will be empty and things like listView1.SelectedIndices[0] will throw IndexOutOfRange exception.
But if you will set MultiSelect property to false. Then this collection will always contain zero or one item. You can use Count property of SelectedIndicesCollection to check if item was selected:
if (listView1.SelectedIndices.Count > 0)
{
int selectedIndex = listView1.SelectedIndices[0];
}
You need to use the selected indices list you can also do this be item too.
listView1.SelectedIndices[0]
First you can get listview item object like below
ListViewItem lst=(ListViewItem)listView.SelectedItems[0];
from that object(lst) you can get the text like below
string text=lst.Content.ToString();
According to MSDN, there is still SelectedIndex. In my opinion your event is wrong, but you can still see it by .SelectedIndex. As it was mentioned before.
UPDATE: according to the comment, the link is fixed for the correct case.
I have a ListView in the itemDetailPage and I would like to share the selected item via e-mail. For which, I need to get the selected item from that listview.
What I tried is
if(listview1.SelectedItems.Count==1)
{
var item = listview1.SelectedItem
}
But I dont get the name, I get only the place it is taken from. Any suggestions?
Use Count instead of count. C# is case sensitive.
if(listview1.SelectedItems.Count == 1)
{
var item = listview1.SelectedItems[0].ToString();
}
Try this. You need to cast SelectedItem with your model class.
if (listview1.SelectedItems.Count==1)
{
var item = listview1.SelectedItems[0] as Node
}
I would like to display text that is stored in my list.
I have the index number, and would like to use the index number to get the item from the collection.
Thx
If it's a List<string>, why not just use:
string item = list[index];
?
You can use ElementAt:
var item = list.ElementAt(3);
Or simply use the indexer, like:
var item = list[3];
I have a ListView in Virtual Mode. I wanna to access SelectedItems property.
But when I use ListView1.SelectedItems , I receive the following Exception :
Cannot access the selected items collection when the ListView is in virtual mode
How can I access to ListView1.SelectedItems in VirtualMode.
It is quite old post but maybe someone else will benefit.
Simply use ListView.SelectedIndexCollection col = listView.SelectedIndices;
Then you can access an item:
forearch(var item in col)
{
string txt = listView.Items[item].Text;
}
..but you won't be able to iterate through ListView.Items using foreach because there is no iterator available in this mode. Using indexer is just flying fine :-)
When trying to use foreach you get an exception:
When the ListView is in virtual mode, you cannot enumerate through the
ListView items collection using an enumerator or call GetEnumerator.
Use the ListView items indexer instead and access an item by index
value.
From the docs
In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.
I you store all items in list and use this list to give item in RetrieveVirtualItem
you can find selected items like following
Dim lstData As List(Of ListViewItem) = New List(Of ListViewItem)
Dim lstSelectedItems As List(Of ListViewItem) = lstData.FindAll(Function(lstItem As ListViewItem) lstItem.Selected = True)
Me.Text = lstItems.Count.ToString()
I've done it by the following code, but it has an exception when more than one item are selected:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
List<ListViewItem> ListViewItems = new List<ListViewItem>();
foreach (int index in listView1.SelectedIndices)
{
ListViewItem SelectedListViewItem = listView1.Items[index]; // exception
ListViewItems.RemoveAt(index);
}
…
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = ListViewItems[e.ItemIndex];
}
Whenever you remove item(s) from a collection, always iterate from the largest index to the smallest index, like this:
for (int index = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
…
}
This is because every time you remove an item in a collection, the index will change if you do not iterate from the largest to the smallest index.