您很容易需要另一个列表来将找到的索引保存到您从原始列表中搜索的内容中。我的 2 列表是结果,我的原始列表是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;
}