如何删除特定文本/消息在ListView显示? [英] how to delete a specific text / message showed in a ListView ?
问题描述
嘿!
我会试着解释我的问题更好地在这里和我的情况:)
i will try to explain my question better here and my situation :)
我有一个的ListView
我的应用程序,这的ListView
用于输出错误,所以它有点用错误日志。
i have a ListView
in my application, this ListView
is used for output errors , so its kinda used for error log.
我的方案主要是一个定时器,即检查在INT值的变化。
My program is mainly a timer, that is checking for changes in int values.
。
问题是,该错误将然后像3-4次,也许更多加入。
when an error occurs I want to show it in my list. Problem is that the error will then be added like 3-4 times, maybe more.
我如果做一个检查,如果(exist.in.listbox)我将无法添加相同的错误第二次。我想要做的就是删除特定的消息/错误,然后将其删除时,其固定或不再对系统构成威胁。
这里是code片断:
If i do a check if(exist.in.listbox) i will not be able to add the same error second time. What I want to do is to delete specific message/error and then delete it when its "fixed" or no longer a threat to the system. here is code snippet:
if (Convert.ToInt32(x) == 1 && Convert.ToInt32(y) == 1)
{
found = false;
ListViewItem item = new ListViewItem(message);
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{ found = true; }
}
if (found == false)
{
item.SubItems.Add(now.ToString());
listView1.Items.Add(item);
listView1.EnsureVisible(item.Index);
}
}
else
{
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{
//no longer a threat, delete the message added aboue
}
}
}
找不到这事。只有当选择 - >删除行
cant find anything about this. only "when selected --> delete the line"
可以考虑一下这款像视觉误差窗口中,当u得到一个红色的错误,U可以修复code.and它disapair:)
can think about this like the error window in visual, when u get a red error, u can fix the code.and it disapair :)
推荐答案
您应该遵循同样的方法添加内容时为。首先,检查如果要执行删除,然后删除该项目。
You should follow the same approach as when you add items. First, check if you want to perform the deletion, and then remove the item.
如果这个效果并不是很好(通过列表中的每个定时器循环迭代两次),你也可以将您的添加/删除的逻辑出的ListView(即可能保持更快的查找字典)。
If this does not perform well (iterating through the list twice on each timer iteration), you could also move your "add/remove" logic out of the ListView (ie, maybe keep a Dictionary for faster lookup).
void DeleteIfNecessary(string message)
{
ListViewItem listViewItem = FindListViewItemForMessage(message);
if (listViewItem == null)
{
// item doesn't exist
return;
}
this.listView1.Items.Remove(listViewItem);
}
private ListViewItem FindListViewItemForMessage(string s)
{
foreach (ListViewItem lvi in this.listView1.Items)
{
if (StringComparer.OrdinalIgnoreCase.Compare(lvi.Text, s) == 0)
{
return lvi;
}
}
return null;
}
这篇关于如何删除特定文本/消息在ListView显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!