根据项目和子项目的文本查找ListView项目 [英] Find ListView Items based on text of items and sub items

查看:96
本文介绍了根据项目和子项目的文本查找ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有listview,有2个组(ONLINEOFFLINE)和两列(NameStatus).

I have listview with 2 groups (ONLINE and OFFLINE) and two columns (Name and Status).

ListView示例

这是我的代码,用于将项目添加到特定组:

This is my code to add items into specific group:

public void f2list(object group, string friend, string status)
{
        ListViewItem item = new ListViewItem(new string[] { friend, (string)status });
        GroupItem(item, group);
        this.ListView.Items.Add(item);
}

object group定义将在哪个组项目中添加OFFLINEONLINEstring friend定义朋友的用户名,string status只是第二列Status

object group defines in which group item will be added OFFLINE or ONLINE , string friend defines friends username and string status is just second column Status

该代码的第二部分:

private void GroupItem(ListViewItem item, object group2)
{
        foreach (ListViewGroup group in this.ListView.Groups)
        {
            if (group.Header == (string)group2)
            {
                item.Group = group;
                break;
            }
        }
}

因此,我需要以某种方式在这两个组之间移动项目,或者从列表中删除项目namestatus,以便可以将其再次添加到另一个组中.

So, I need somehow move items between those two groups or remove item name and status from list so it can be added in another group again.

我知道如何在单击selected项时将其删除,但是当其他类或函数调用移动该特定项/将该特定项分配给另一个组"时,该怎么做?

I know how to remove selected item when I click on it, but how to do that when other class or function call 'move that specific item/assign that specific item to another group'?

f2list由另一个public void AddUserToList调用,但这仅是为了分配f2list(group,username,status);之类的文本(例如f2list("ONLINE","Joe","Hey i am online!");,并且还要检查是否已经存在username以防止重复项.)

This f2list is called by another public void AddUserToList, but this is only to assign text like f2list(group,username,status); (eg. f2list("ONLINE","Joe","Hey i am online!"); and also its check if username already exist to prevent double items.

如何在组之间移动项目取决于f2list(group, ...中调用了哪些组?此group是预定义的OFFLINEONLINE

How to move item between groups depends which groups is called in f2list(group, ...? This group is predefined OFFLINE or ONLINE

那么当Name又名username联机显示在listview和组ONLINE中时,但是当他/她离线时,则分配给另一个组OFFLINE?

So when Name aka username comes online to show in listview and group ONLINE but when he/she goes offline then assign to another group OFFLINE?

其他信息:该代码仅在将项目添加到列表中时有效.但是如何在这两个组之间移动项目?甚至从组为OFFLINEONLINE的列表中删除项目?

Additional info: This code works fine only to add items to the list. But how to move items between those two groups? Or even remove item from list where group is OFFLINE or ONLINE?

编辑另外,当我手动删除selected项目时,该项目也会正确更改组.

EDIT Also when I manually remove selected item this item will change group correctly.

编辑2 这些项目由timer添加,该项目检查其他用户是联机还是脱机.

EDIT 2 Those items are added by timer which checks others users if they are online or offline.

推荐答案

使用FindItemWithText查找项目

要找到基于Text项目的Item,可以使用

To find an Item based on Text of item you can use FindItemWithText

var item = this.listView1.FindItemWithText("item text");

您还可以使用其他签名在搜索中包括子项目:

You can also use other signatures to include sub-items in search:

var item = this.listView1.FindItemWithText("", true, 0);

使用Linq查找项目

还可以使用Linq方法来执行自定义搜索.例如:

Also to perform a custom search, you can use Linq methods. For example:

var item = this.listView1.Items.Cast<ListViewItem>()
                .Where(x => (x.Text == "Some Text" ||
                       x.SubItems[1].Text == "Some Text") &&
                       x.Group.Name=="group1" )
                .FirstOrDefault();

查找组

创建组时,请为组分配合适的名称,然后按名称查找它们.注意NameHeaderText不同.要按名称查找组,您可以:

When creating groups, assign suitable names to groups and then find them by name. Pay attention Name is different from HeaderText. To find a group by name, you can:

var group = this.listView1.Groups["group1"];

将项目移至组

要将Item移至Group,足以找到所需的项目并找到所需的组,然后只需将该组分配给ItemGroup属性:

To move an Item to a Group, it's enough to find the item and find the group you want, then simply assign the group to Group property of Item:

item.Group = group;

这篇关于根据项目和子项目的文本查找ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆