在 C# winform 中删除列表框项目
Deleting listbox items in C# winform
我不知道如何在 c# 中正确地从 winform 列表框中删除项目。
列表框由 FileSystemWatcher
中的一些字符串填充,基本上将修改过的文件放入列表框。
然后我创建了一个 "Search" 函数来删除不包含用户在文本框中输入的内容的项目。
这是代码
private void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text) && lstFileEvents.Items.Count > 0)
{
for (int i = 0; i < lstFileEvents.Items.Count; i++)
{
if (!lstFileEvents.Items[i].ToString().Contains(txtSearch.Text))
{
lstFileEvents.Items.RemoveAt(i);
}
}
lstFileEvents.Refresh();
}
}
实际上我尝试了很多方法,查看了各种 Whosebug 问题和 google 结果如下:
- 使用 .RemoveAt()
- 使用 Update() 而不是 Refresh()
- 清除列表框并直接更新listbox.DataSource,然后refreshing/update
- 创建一个虚拟对象 ListBox.ObjectCollection,在其上应用 search/remove,然后将其指定为数据源和 refreshing/update。
- 其他不记得了
嗯,没有任何效果。该列表保留在那里,调试没有帮助。
我做错了什么?
编辑:
列表框人口代码:
void fswFileWatch_Renamed(object sender, RenamedEventArgs e)
{
fswFileWatch.EnableRaisingEvents = false;
WriteListbox("Renamed: ", e);
fswFileWatch.EnableRaisingEvents = true;
}
public void WriteListbox(string msg, FileSystemEventArgs e)
{
//Some filter which works fine
if (!String.IsNullOrEmpty(txtExcludeFilter.Text))
{
foreach (string Filter in txtExcludeFilter.Text.Split(','))
{
//some other filter
if (!e.FullPath.Contains(Filter))
{
//here's where I populate the list
lstFileEvents.Items.Add(msg + e.FullPath);
}
}
}
else
{
lstFileEvents.Items.Add(msg + e.FullPath);
}
}
Here 建议反向删除列表项。
第一组:
listBox1.SelectionMode = SelectionMode.MultiExtended;
然后反向删除:
for (int i = listBox1.SelectedIndices.Count-1; i >= 0; i--)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
}
我不知道如何在 c# 中正确地从 winform 列表框中删除项目。
列表框由 FileSystemWatcher
中的一些字符串填充,基本上将修改过的文件放入列表框。
然后我创建了一个 "Search" 函数来删除不包含用户在文本框中输入的内容的项目。
这是代码
private void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text) && lstFileEvents.Items.Count > 0)
{
for (int i = 0; i < lstFileEvents.Items.Count; i++)
{
if (!lstFileEvents.Items[i].ToString().Contains(txtSearch.Text))
{
lstFileEvents.Items.RemoveAt(i);
}
}
lstFileEvents.Refresh();
}
}
实际上我尝试了很多方法,查看了各种 Whosebug 问题和 google 结果如下:
- 使用 .RemoveAt()
- 使用 Update() 而不是 Refresh()
- 清除列表框并直接更新listbox.DataSource,然后refreshing/update
- 创建一个虚拟对象 ListBox.ObjectCollection,在其上应用 search/remove,然后将其指定为数据源和 refreshing/update。
- 其他不记得了
嗯,没有任何效果。该列表保留在那里,调试没有帮助。
我做错了什么?
编辑:
列表框人口代码:
void fswFileWatch_Renamed(object sender, RenamedEventArgs e)
{
fswFileWatch.EnableRaisingEvents = false;
WriteListbox("Renamed: ", e);
fswFileWatch.EnableRaisingEvents = true;
}
public void WriteListbox(string msg, FileSystemEventArgs e)
{
//Some filter which works fine
if (!String.IsNullOrEmpty(txtExcludeFilter.Text))
{
foreach (string Filter in txtExcludeFilter.Text.Split(','))
{
//some other filter
if (!e.FullPath.Contains(Filter))
{
//here's where I populate the list
lstFileEvents.Items.Add(msg + e.FullPath);
}
}
}
else
{
lstFileEvents.Items.Add(msg + e.FullPath);
}
}
Here 建议反向删除列表项。 第一组:
listBox1.SelectionMode = SelectionMode.MultiExtended;
然后反向删除:
for (int i = listBox1.SelectedIndices.Count-1; i >= 0; i--)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
}