Expand|Select|Wrap|Line Numbers
- private void treeView1_DragDrop(object sender, DragEventArgs e)
- {
- // Retrieve the client coordinates of the drop location.
- Point targetPoint = trvDocStyles.PointToClient(new Point(e.X, e.Y));
- TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
- if ((draggedNode.Tag.GetType()) == (typeof(DocStyles)))//error line
- return;
- // Retrieve the node at the drop location.
- TreeNode targetNode = trvDocStyles.GetNodeAt(targetPoint);
- //TreeNode targetNode = null;
- targetNode = trvDocStyles.GetNodeAt(targetPoint);
- if (targetNode.Tag.GetType() == typeof(DocParas))
- {
- targetNode = targetNode.Parent;
- }
- // Retrieve the node that was dragged.
- // Confirm that the node at the drop location is not
- // the dragged node or a descendant of the dragged node.
- DocStyles targetstyle = objDocStyles.Find(c => c.StyleID == (targetNode.Tag as DocStyles).StyleID);
- DocParas draggpara = objdocpara.Find(c => c.ParaID == (draggedNode.Tag as DocParas).ParaID);
- draggpara.CurentStyleID = targetstyle.StyleID;
- int parcnt = 0;
- parcnt = objdocpara.Where(c => c.ParaStyleID == targetstyle.StyleID || c.CurentStyleID == targetstyle.StyleID).Count();
- targetNode.Text = (parcnt > 1) ? targetstyle.DispStyleName + " - ( " + parcnt + " )" : targetstyle.DispStyleName;
- DocStyles currentstyle = objDocStyles.Find(c => c.StyleID == draggpara.ParaStyleID);
- // Confirm that the node at the drop location is not
- // the dragged node or a descendant of the dragged node.
- 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((TreeNode)draggedNode.Clone());
- }
- // Expand the node at the location
- // to show the dropped node.
- targetNode.Expand();
- }
- }
- // Determine whether one node is a parent
- // or ancestor of a second node.
- private bool ContainsNode(TreeNode node1, TreeNode node2)
- {
- // Check the parent node of the second node.
- if (node2.Parent == null) return false;
- if (node2.Parent.Equals(node1)) return true;
- // If the parent node is not null or equal to the first node,
- // call the ContainsNode method recursively using the parent of
- // the second node.
- return ContainsNode(node1, node2.Parent);
- }
i am getting an error object reference not set to an instance of an object