【发布时间】:2020-11-17 17:03:20
【问题描述】:
我想在列表视图中搜索 ic# 当我在搜索框中写东西时,我的文本出现在列表视图中并向我显示我需要的项目,但是当我从搜索文本框中删除文本时,我看不到我的旧数据在列表视图中我的代码是:
private bool ItemMatches(ListViewItem item, string text)
{
bool matches = false;
matches |= item.Text.ToLower().Contains(text.ToLower());
if (matches)
{
return true;
}
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
matches |= subitem.Text.ToLower().Contains(text.ToLower());
if (matches)
{
return true;
}
}
return false;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
listView1.BeginUpdate();
// restore all items in case user deletes some characters in the textbox
// ReinitList();
string search = textBox4.Text;
// Search items in our Jobs ListView, remove those that do not match search
if (search != String.Empty)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
ListViewItem currentItem = listView1.Items[i];
if (ItemMatches(currentItem, search))
{
// highlight, or move highlighting to ItemMatches
}
else
{
listView1.Items.RemoveAt(i);
}
}
}
listView1.EndUpdate();
}
【问题讨论】:
-
您正在删除项目。一旦删除,你就完成了。尝试将项目保留在列表中,然后将它们添加回来
-
我会怎么做你能解释一下
标签: c# windows winforms listview search