/// <summary> /// 刷新左侧列表 /// </summary> private void RefreshUserList() { ItemKeyValue newer = null; ItemKeyValue older = null; for (int i = 0; i < this.clientListBox.Items.Count; i++) { ItemKeyValue item = (ItemKeyValue)this.clientListBox.Items[i]; if (item.Name != ((User)item.Value).name) { newer = new ItemKeyValue(((User)item.Value).name, (User)item.Value); older = item; break; } } if (newer != null) { int index = this.clientListBox.Items.IndexOf(older); if (this.clientListBox.InvokeRequired) { Action <int> update = x => { this.clientListBox.Items.RemoveAt(index); this.clientListBox.Items.Insert(index, newer); }; this.clientListBox.Invoke(update, index); } else { this.clientListBox.Items.RemoveAt(index); this.clientListBox.Items.Insert(index, newer); } } }
/// <summary> /// 删除客户端 /// </summary> /// <param name="toDeleteUser"></param> public void DelClientListBoxItem(User toDeleteUser) { if (clientListBox.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action <User> actionDelegate = (x) => { ItemKeyValue tmp = null; foreach (ItemKeyValue item in this.clientListBox.Items) { if (((User)item.Value).id == x.id) { tmp = item; break; } } if (tmp != null) { this.clientListBox.Items.Remove(tmp); } }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; this.msgHouse.Invoke(actionDelegate, toDeleteUser); } else { ItemKeyValue tmp = null; foreach (ItemKeyValue item in this.clientListBox.Items) { if (((User)item.Value).id == toDeleteUser.id) { tmp = item; break; } } if (tmp != null) { this.clientListBox.Items.Remove(tmp); } } }
/// <summary> /// 刷新左侧列表 /// </summary> private void RefreshUserList() { ItemKeyValue newer = null; ItemKeyValue older = null; for (int i = 0; i < this.clientListBox.Items.Count; i++) { ItemKeyValue item = (ItemKeyValue)this.clientListBox.Items[i]; if (item.Name != ((User)item.Value).name) { newer = new ItemKeyValue(((User)item.Value).name, (User)item.Value); older = item; break; } } if (newer != null) { int index = this.clientListBox.Items.IndexOf(older); if (this.clientListBox.InvokeRequired) { Action<int> update = x => { this.clientListBox.Items.RemoveAt(index); this.clientListBox.Items.Insert(index, newer); }; this.clientListBox.Invoke(update, index); } else { this.clientListBox.Items.RemoveAt(index); this.clientListBox.Items.Insert(index, newer); } } }