Search for ListViewItem in VirtualMode

812 Views Asked by At

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.

2

There are 2 best solutions below

5
Ivan Stoev On

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();
}
0
liberty On

it's easy you need another List to save the found index's to the stuff you searched for from your original List. Mine 2 List is result und my original list is m_sTable1_DataList.

private List<int> result = null;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(e.KeyChar == '\r')
        {
            string sSearchValue = textBox1.Text.TrimEnd();                      

            if (sSearchValue.Length > 0)
            {     

                listViewDB1.FindItemWithText(sSearchValue);
                //Select the item found and scroll it into view.
            }
        }
    }
        private void listViewDB1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
        {
            result = Enumerable.Range(0, m_sTable1_DataList.Count)
         .Where(i => m_sTable1_DataList[i].Contains(textBox1.Text.ToString().TrimEnd()))
         .ToList();
            if (result.Count > 0)
                listViewDB1.VirtualListSize = result.Count;
            else
            {
                result = null;      
                this.listViewDB1.VirtualListSize = 0;
            }
        }
    private void listViewDB1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        if (result != null)
            e.Item = new ListViewItem(m_sTable1_DataList[result[e.ItemIndex]]);
        else
            e.Item = new ListViewItem(m_sTable1_DataList[e.ItemIndex]);
    }
i have a reset button to come back to the orginal list and VirtualListSize.
private void Resetbutton_Click(object sender, EventArgs e)
    {      
        listViewDB2.Items.Clear();
        textBox1.Text = "";
        textBox1.Refresh();  
        result = null;
        this.listViewDB1.VirtualListSize = nCountRow;
    }