public void RemoveChild(TreeNodeBase child) { NodeEventArgs eventArgs = new NodeEventArgs(child); this.OnChildRemoving(eventArgs); if (eventArgs.Cancel) { return; } this.children.Remove(child); }
private void ChildAdding(object sender, NodeEventArgs e) { if (e.Node.Children.Count > 0) { throw new InvalidOperationException("You can only add nodes that don't have children"); // Alternatively, you could recursively get the children, set up the added/removed events and add to the index } e.Node.ChildAdding += new EventHandler <NodeEventArgs>(ChildAdding); e.Node.ChildRemoving += new EventHandler <NodeEventArgs>(ChildRemoving); Index.Add(this.GetNodeIndex(e.Node), e.Node); }
private void ChildRemoving(object sender, NodeEventArgs e) { if (e.Node.Children.Count > 0) { throw new InvalidOperationException("You can only remove leaf nodes that don't have children"); // Alternatively, you could recursively get the children, remove the events and remove from the index } // Remove from the index. Remove events in case user code keeps reference to this node and later adds/removes children e.Node.ChildAdding -= new EventHandler <NodeEventArgs>(ChildAdding); e.Node.ChildRemoving -= new EventHandler <NodeEventArgs>(ChildRemoving); Index.Remove(this.GetNodeIndex(e.Node)); }
/// <summary> /// Adds a child node to the current node /// </summary> /// <param name="child">The child to add.</param> /// <returns>The child node, if it was added. Useful for chaining methods.</returns> public TreeNodeBase AddChild(TreeNodeBase child) { NodeEventArgs eventArgs = new NodeEventArgs(child); this.OnChildAdding(eventArgs); if (eventArgs.Cancel) { return(null); } this.children.Add(child); return(child); }