private TreeNode _fastRemoveNodes(MultiSelectTreeViewDoEvents treeView, int removeNodesCount, IEnumerable <TreeNode> removeNodes) { TreeNode nextNode = null; if (treeView.Nodes.Count == removeNodesCount) { _fastClearNodes(treeView); } else { var removeNodesIndexes = _createSortNodeIndexs(removeNodes); var lastRemoveNodeIndex = removeNodesIndexes.Count == 0 ? -1 : removeNodesIndexes[0]; nextNode = _getNextTreeNode((lastRemoveNodeIndex > -1 && lastRemoveNodeIndex < treeView.Nodes.Count) ? treeView.Nodes[lastRemoveNodeIndex] : null); if (_availableSubstitute(removeNodesCount)) { var nodes = _createTreeNodeArray(treeView, removeNodesIndexes, nextNode?.Index ?? -1, out nextNode); var newTreeView = _substituteTreeView(treeView); newTreeView.Nodes.AddRange(nodes); _switchTreeViews(treeView, newTreeView); } else { treeView.BeginUpdate(); foreach (var removeNodesIndex in removeNodesIndexes.Where(removeNodesIndex => (removeNodesIndex > -1) && (removeNodesIndex < treeView.Nodes.Count))) { treeView.Nodes.RemoveAt(removeNodesIndex); } treeView.EndUpdate(); } } return(nextNode); }
private static void _RemoveTreeView(MultiSelectTreeViewDoEvents treeView) { if (!treeView.IsDisposed) { var action = new Action(() => { treeView.Parent = null; treeView.Size = new Size(1, 1); treeView.BeginUpdate(); while (treeView.Nodes.Count != 0) { treeView.RemoveAt(treeView.Nodes.Count - 1); } treeView.EndUpdate(); treeView.Dispose(); GC.Collect(); }); try { if (treeView.InvokeRequired) { treeView.Invoke(() => action()); } else { action(); } } catch (Exception) { } } }