I have a ListView on my server's GUI which is filled with connected clients data and I have to keep this data fresh. For example add new item for new client connection or remove item of disconnected client etc. I wrote this:
Expand|Select|Wrap|Line Numbers
- /**
- * This fills the ListView with new data. Client is class with getter of
- * IP adress, client's ID, name and socket. Clients is a Hashtable filled
- * by instances of Client class (HT Key is (Double)Id, Value (Client)
- * new Client(...))
- *
- * The first loop works fine, if the Client is not in LstvClients, it will be
- * added.
- */
- foreach (IMServer.Core.Client Client in Clients.Values)
- {
- ListViewItem ListItem = new ListViewItem(Convert.ToString(Client.Id));
- ListItem.Name = Convert.ToString(Client.Id);
- ListItem.SubItems.Add(Client.Name);
- ListItem.SubItems.Add(Client.IpAddress);
- if (!LstvClients.Items.ContainsKey(ListItem.Name))
- {
- LstvClients.Items.Add(ListItem);
- }
- }
- /**
- * But this not. It should remove the item of disconnected client
- * (client isn't in Clients Hashtable, removed by server). This code
- * remove all clients from ListView.
- */
- foreach (ListViewItem Item in LstvClients.Items)
- {
- if (!Clients.ContainsKey(Item.Name))
- {
- LstvClients.Items.Remove(Item);
- }
- }
RiZe