Wasabee Wasabee - 3 months ago 39
C# Question

Find ListView Items based on text of items and sub items

I have

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

ListView Example

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
defines in which group item will be added
OFFLINE
or
ONLINE
,
string friend
defines friends username and
string status
is just second column
Status


Second part of that code:

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


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.

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'?

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.

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


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
?

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
?

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

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

Answer

Find an Item using FindItemWithText

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);

Find an Item using 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();

Find a Group

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"];

Move an Item to a Group

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;