// Sort the collection by calling the Text property private void TreeListView_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e) { e.CancelEdit = true; if (e.Label == null || e.Label == "") { return; } TreeListViewItem item = (TreeListViewItem)base.Items[e.Item]; item.Text = e.Label; }
/// <summary> /// Check if this node is one of the parents of an item (recursively) /// </summary> /// <param name="item"></param> /// <returns></returns> public bool IsAParentOf(TreeListViewItem item) { TreeListViewItemCollection parents = item.ParentsInHierarch; foreach (TreeListViewItem parent in parents) { if (parent == this) { return(true); } } return(false); }
private int GetInsertTreeListViewIndex(TreeListViewItem item) { if (TreeListView == null) { return(-1); } ReadWriteLock.AcquireReaderLock(-1); int collectionindex = GetInsertCollectionIndex(item); if (Owner != null) { int a = 0; a++; } int index = -1; // First level item (no parent) if (Owner != null && collectionindex != -1) { if (collectionindex == 0) { index = 0; } else { index = this[collectionindex - 1].LastChildIndexInListView + 1; } } else if (Parent != null && collectionindex != -1) { if (!Parent.Visible || !Parent.IsExpanded) { index = -1; } else { if (collectionindex == 0) { index = Parent.Index + 1; } else { index = Parent.Items[collectionindex - 1].LastChildIndexInListView + 1; } } } ReadWriteLock.ReleaseReaderLock(); return(index); }
/// <summary> /// Expand /// </summary> public void Expand() { // The item wasn't expanded -> raise an event if (Visible && !_isexpanded && ListView != null) { TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs( this, TreeListViewAction.Expand); ListView.RaiseBeforeExpand(e); if (e.Cancel) { return; } } if (this.Visible) { Items.ReadWriteLock.AcquireReaderLock(-1); for (int i = Items.Count - 1; i >= 0; i--) { TreeListViewItem item = this.Items[i]; if (!item.Visible) { ListView LView = this.ListView; LView.Items.Insert( this.Index + 1, item); item.SetIndentation(); } if (item.IsExpanded) { item.Expand(); } } Items.ReadWriteLock.ReleaseReaderLock(); } // The item wasn't expanded -> raise an event if (Visible && !_isexpanded && ListView != null) { this._isexpanded = true; TreeListViewEventArgs e = new TreeListViewEventArgs( this, TreeListViewAction.Expand); ListView.RaiseAfterExpand(e); if (AfterExpand != null) { AfterExpand(this); } } this._isexpanded = true; }
/// <summary> /// Returns true if this collection contains an item /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Contains(TreeListViewItem item) { bool res = false; ReadWriteLock.AcquireReaderLock(-1); foreach (TreeListViewItem elt in this) { if (item == elt) { res = true; break; } } ReadWriteLock.ReleaseReaderLock(); return(res); }
/// <summary> /// Remove an item from the collection and the TreeListView /// </summary> /// <param name="item"></param> public virtual void Remove(TreeListViewItem item) { int index = -1; ReadWriteLock.AcquireWriterLock(-1); for (int i = 0; i < this.Count; i++) { if (this[i] == item) { index = i; } } if (index > -1) { RemoveAt(index); } ReadWriteLock.ReleaseWriterLock(); }
/// <summary> /// Adds an item in the collection and in the TreeListView /// </summary> /// <param name="item"></param> /// <returns>Index of the item in the collection</returns> public virtual int Add(TreeListViewItem item) { int index = GetInsertCollectionIndex(item); if (index == -1) { return(-1); } if (Parent != null) { item.SetParent(Parent); } item.Items.Comparer = this.Comparer; ReadWriteLock.AcquireWriterLock(-1); int treelistviewindex = GetInsertTreeListViewIndex(item); // Insert in the ListView if (treelistviewindex > -1) { ListView listview = (ListView)TreeListView; InsertListViewItem insert = new InsertListViewItem(listview.Items.Insert); if (listview.InvokeRequired) { listview.Invoke(insert, new object[] { treelistviewindex, (ListViewItem)item }); } else { listview.Items.Insert(treelistviewindex, (ListViewItem)item); } if (item.IsExpanded) { item.Expand(); } item.SetIndentation(); } // Insert in this collection if (index > -1) { List.Insert(index, item); } ReadWriteLock.ReleaseWriterLock(); return(index); }
/// <summary> /// Remove an item from the collection and the TreeListView /// </summary> /// <param name="index"></param> public new void RemoveAt(int index) { ReadWriteLock.AcquireWriterLock(-1); TreeListViewItem item = this[index]; if (this[index].Visible && this.TreeListView != null) { ListView listview = (ListView)TreeListView; RemoveListViewItem remove = new RemoveListViewItem(listview.Items.Remove); if (listview.InvokeRequired) { listview.Invoke(remove, new Object[] { (ListViewItem)item }); } else { listview.Items.Remove((ListViewItem)item); } } List.RemoveAt(index); item.SetParent(null); ReadWriteLock.ReleaseWriterLock(); }
/// <summary> /// Compare two TreeListViewItems /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int Compare(object x, object y) { TreeListViewItem a = (TreeListViewItem)x; TreeListViewItem b = (TreeListViewItem)y; int res = 0; try { res = string.CompareOrdinal(a.SubItems[Column].Text.ToUpper(), b.SubItems[Column].Text.ToUpper()); } catch {} switch (SortOrder) { case SortOrder.Ascending: return(res); case SortOrder.Descending: return(-res); default: return(0); } }
/// <summary> /// Create a collection within a TreeListViewItem /// </summary> /// <param name="parent"></param> public TreeListViewItemCollection(TreeListViewItem parent) { _parent = parent; }
/// <summary> /// Index of an item /// </summary> /// <param name="item"></param> /// <returns></returns> public int IndexOf(TreeListViewItem item) { return(base.IndexOf((ListViewItem)item)); }
private void TreeListView_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e) { TreeListViewItem item = (TreeListViewItem)base.Items[e.Index]; item.Check(e.NewValue == CheckState.Checked); }
internal void SetParent(TreeListViewItem parent) { _parent = parent; }
/// <summary> /// Create a new instance of TreeListViewCancelEvent arguments /// </summary> /// <param name="item"></param> /// <param name="action"></param> public TreeListViewCancelEventArgs(TreeListViewItem item, TreeListViewAction action) : base(item, action) { }
/// <summary> /// Create a new instance of TreeListViewEvent arguments /// </summary> /// <param name="item"></param> /// <param name="action"></param> public TreeListViewEventArgs(TreeListViewItem item, TreeListViewAction action) { _item = item; _action = action; }
/// <summary> /// Returns true if the specified item is in the collection /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Contains(TreeListViewItem item) { return(base.Contains((ListViewItem)item)); }
/// <summary> /// WndProc /// </summary> /// <param name="m"></param> protected override void WndProc(ref System.Windows.Forms.Message m) { TreeListViewItem item = null; switch ((WM_MESSAGE)m.Msg) { // Disable this notification to remove the auto-check when // the user double-click on an item case WM_MESSAGE.WM_LBUTTONDBLCLK: if (this.SelectedIndices.Count > 0) { item = this.SelectedItems[0]; bool doExpColl = false; Rectangle rec; switch (ExpandMethod) { case TreeListViewExpandMethod.IconDbleClick: rec = item.GetBounds(ItemBoundsPortion.Icon); if (rec.Contains(PointToClient(MousePosition))) { doExpColl = true; } break; case TreeListViewExpandMethod.ItemOnlyDbleClick: rec = item.GetBounds(ItemBoundsPortion.ItemOnly); if (rec.Contains(PointToClient(MousePosition))) { doExpColl = true; } break; case TreeListViewExpandMethod.EntireItemDbleClick: rec = item.GetBounds(ItemBoundsPortion.Entire); if (rec.Contains(PointToClient(MousePosition))) { doExpColl = true; } break; default: break; } if (doExpColl) { FindForm().Cursor = Cursors.WaitCursor; BeginUpdate(); if (item.IsExpanded) { item.Collapse(); } else { item.Expand(); } EndUpdate(); FindForm().Cursor = Cursors.Default; } } return; case WM_MESSAGE.WM_KEYDOWN: if (this.SelectedIndices.Count == 0) { break; } item = this.SelectedItems[0]; if (item == null) { break; } switch ((Keys)(int)m.WParam) { case Keys.Enter: break; case Keys.Left: this.BeginUpdate(); if (item.IsExpanded) { item.Collapse(); } else if (item.Parent != null) { item.Parent.Selected = true; item.Parent.Focused = true; } this.EndUpdate(); break; case Keys.Right: if (item.Items.Count == 0) { break; } this.BeginUpdate(); if (!item.IsExpanded) { item.Expand(); } else { item.Items[item.Items.Count - 1].Selected = true; item.Items[item.Items.Count - 1].Focused = true; } this.EndUpdate(); break; } return; } base.WndProc(ref m); }