-1

I'm having some trouble doing a search in a ListView when using VirtualMode. The ListView populates just find using RetrieveVirtualItem event.

I have a text box and "Search" button on my form.

private void btnSearch_Click(object sender, EventArgs e)
{
    listViewFields.FindItemWithText(txtSearch.Text);
}

I have handled the SearchForVirtualItem event that looks for the text in my collection and sets the index to the Index property of the event args.

private void listViewFields_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
        e.Index = collection.IndexOf(e.Text);
}

The value of e.Index does get set to the expected value but then nothing happens in my ListView.

James
  • 319
  • 3
  • 19
  • have you considered looking at the MSDN four [ListView.SearchForVirtualItem Event](https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.searchforvirtualitem(v=vs.110).aspx) – MethodMan Jun 28 '16 at 17:53
  • That's what I'm using. If you notice my second code block I'm handling that event. The FindItemWithText method triggers that event and the value of e.Text in the event args is the value I passed to FindItemWithText. Nothing happens. – James Jun 28 '16 at 17:57
  • http://stackoverflow.com/questions/27129619/how-to-use-finditemwithtext – MethodMan Jun 28 '16 at 18:00
  • The answer given in that link is exactly what I'm doing. It isn't working. – James Jun 28 '16 at 18:03

1 Answers1

0

The value of e.Index does get set to the expected value but then nothing happens in my ListView.

The FindItemWithText method does exactly what it says - finds and returns the first ListViewItem that begins with the specified text value.

In order something to happen in your list view, you have to do something with the returned item. For instance:

var item = listViewFields.FindItemWithText(txtSearch.Text);
if (item != null)
{
    listViewFields.FocusedItem = item;
    item.Selected = true;
    item.EnsureVisible();
}
Ivan Stoev
  • 159,890
  • 9
  • 211
  • 258
  • Does it return something? – Ivan Stoev Jun 28 '16 at 18:15
  • Yes, it does. It returns the item I was expecting. The problem is that the list view doesn't go to that item. – James Jun 28 '16 at 18:19
  • It depends what do you mean by "go to that item". The above is just example, it really focuses the item, but you don't see it until the focus goes to the list view. Again, the action depends on what do you want to happen. – Ivan Stoev Jun 28 '16 at 18:23
  • I want the item that matches the search condition to become visible within the ListView. So let's say I've at the top of the list and the first 10 list items are showing. I search for an item that is the 200th item in the list. I want the ListView to jump to the 200th item. – James Jun 28 '16 at 18:26
  • Well, this was noit mentioned in the question. Does the update work for you? – Ivan Stoev Jun 28 '16 at 18:31