Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        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();
            }
        }