private void treeview_Shift(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.NumPad3) { TreeView treeView = sender as TreeView; MyTreeNode selectedNode = (MyTreeNode)treeView.SelectedNode; MyTreeNode cNode = selectedNode; MyTreeNode pNode = (MyTreeNode)selectedNode.Parent; int i = selectedNode.Index; //change index based on which key is pressed if (e.KeyCode == Keys.NumPad9) { i--; } if (e.KeyCode == Keys.NumPad3) { i++; } //Remove old node from tree & data selectedNode.Remove(); pNode.nNode.RemoveNode(selectedNode.nNode); //Add the copy to parent with new index pNode.Nodes.Insert(i, cNode); pNode.nNode.InsertNode(i, cNode.nNode); treeView.SelectedNode = cNode; } }
private void btnVerwijderen_Click(object sender, EventArgs e) { //set sender as toolstrip item to make properties accessable ToolStripItem button = sender as ToolStripItem; //then ask for the parent and put it in pbutton ContextMenuStrip pbutton = (ContextMenuStrip)button.GetCurrentParent(); //then put the parent's tag into treeview TreeView treeView = (TreeView)pbutton.Tag; MyTreeNode selectedNode = (MyTreeNode)treeView.SelectedNode; MyTreeNode pNode = (MyTreeNode)selectedNode.Parent; pNode.nNode.RemoveNode(selectedNode.nNode); selectedNode.Remove(); }
private void treeView_lib_f2_DragDrop(object sender, DragEventArgs e) { TreeView treeView = sender as TreeView; // Retrieve the client coordinates of the drop location. Point targetPoint = treeView.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location. MyTreeNode targetNode = (MyTreeNode)treeView.GetNodeAt(targetPoint); // Retrieve the node that was dragged. MyTreeNode draggedNode = (MyTreeNode)e.Data.GetData(typeof(MyTreeNode)); // Confirm that the node at the drop location is not // the dragged node or a descendant of the dragged node. if (targetNode == null) { targetNode = (MyTreeNode)treeView.Nodes[0]; //als jij niet op een node sleept, targetNode wordt rootnode } if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode)) { // If it is a move operation, remove the node from its current // location and add it to the node at the drop location. if (e.Effect == DragDropEffects.Move) { draggedNode.Remove(); targetNode.Nodes.Add(draggedNode); } // If it is a copy operation, clone the dragged node // and add it to the node at the drop location. else if (e.Effect == DragDropEffects.Copy) { targetNode.Nodes.Add((MyTreeNode)draggedNode.Clone()); } // Expand the node at the location // to show the dropped node. targetNode.Expand(); } }
public void treeView_DragDrop(object sender, DragEventArgs e) { TreeView treeView = sender as TreeView; // Retrieve the client coordinates of the drop location. Point targetPoint = treeView.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location. MyTreeNode targetNode = (MyTreeNode)treeView.GetNodeAt(targetPoint); // Retrieve the node that was dragged. MyTreeNode draggedNode = (MyTreeNode)e.Data.GetData(typeof(MyTreeNode)); // Confirm that the node at the drop location is not // the dragged node or a descendant of the dragged node. if (targetNode == null) { targetNode = (MyTreeNode)treeView.Nodes[0]; //als jij niet op een node sleept, targetNode wordt rootnode } if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode)) { // If it is a move operation, remove the node from its current // location and add it to the node at the drop location. if (e.Effect == DragDropEffects.Move) { if (targetNode.nNode.GetClass() == "Signal" && draggedNode.nNode.GetClass() == "Signal") { Signal sTarget = (Signal)targetNode.nNode; Signal sDragged = (Signal)draggedNode.nNode; sTarget.Connect(sDragged); MyTreeNode t = (MyTreeNode)targetNode.Parent; Device parentD = (Device)t.nNode; //System.Console.WriteLine("Connected: "+sTarget.ConnectedSignal.sNode + sDragged.ConnectedSignal.sNode); targetNode.ForeColor = System.Drawing.Color.Green; draggedNode.ForeColor = System.Drawing.Color.Green; //refresh tree MyTreeNode treeRoot = (MyTreeNode)treeView_proj.Nodes[0]; Node root = treeRoot.nNode; loadColors(treeRoot); treeView_sig.Nodes.Clear(); PopulateSigTree(root); //sTarget.Disconnnect(sDragged); //System.Console.WriteLine("Connected: " + sTarget.ConnectedSignal.sNode + sDragged.ConnectedSignal.sNode); } if ((targetNode.nNode.GetClass() == "Device" && draggedNode.nNode.GetClass() != "Signal") || (targetNode.nNode.GetClass() == "Node" && draggedNode.nNode.GetClass() == "Signal") || (targetNode.nNode.GetClass() == "Signal")) { return; } else { //Remove and Add node in Data MyTreeNode parentNode = (MyTreeNode)draggedNode.Parent; // MyTreeNode cloneNode = (MyTreeNode)draggedNode.Clone(); if (parentNode == null) { return; } parentNode.nNode.RemoveNode(draggedNode.nNode); targetNode.nNode.AddNode(draggedNode.nNode); //Remove and Add node in the tree //Always do this after manipulating the data, otherwise the parentNode is not accurate draggedNode.Remove(); targetNode.Nodes.Add(draggedNode); //Put dragged node on top (bottom is default) } } // Expand the node at the location // to show the dropped node. targetNode.Expand(); } }