Moving Nodes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Moving Nodes

    I've got a problem, i got the nodes to move but
    1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes)
    2. i want to make the nodes, if moved update the new nodes locations

    This is my coding
    Code:
     private void tvDescriptions_MouseDown(object sender, MouseEventArgs e)
            {
                TreeView tree = (TreeView)sender;
    
                // Get the node underneath the mouse.
                TreeNode node = tree.GetNodeAt(e.X, e.Y);
    
                // Start the drag-and-drop operation with a cloned copy of the node.
                if (node != null)
                {
                    tree.DoDragDrop(node.Clone(), DragDropEffects.Copy);
                }
            }
    
            private void tvDescriptions_DragOver(object sender, DragEventArgs e)
            {
                // Get the tree.
                TreeView tree = (TreeView)sender;
    
                // Drag and drop denied by default.
                e.Effect = DragDropEffects.None;
    
                // Is it a valid format?
                if (e.Data.GetData(typeof(TreeNode)) != null)
                {
                    // Get the screen point.
                    Point pt = new Point(e.X, e.Y);
    
                    // Convert to a point in the TreeView's coordinate system.
                    pt = tree.PointToClient(pt);
    
                    // Is the mouse over a valid node?
                    TreeNode node = tree.GetNodeAt(pt);
                    if (node != null)
                    {
                        e.Effect = DragDropEffects.Copy;
                        tree.SelectedNode = node;
                    }
                }
            }
    
            private void tvDescriptions_DragDrop(object sender, DragEventArgs e)
            {
                // Get the tree.
                TreeView tree = (TreeView)sender;
    
                // Get the screen point.
                Point pt = new Point(e.X, e.Y);
    
                // Convert to a point in the TreeView's coordinate system.
                pt = tree.PointToClient(pt);
    
                // Get the node underneath the mouse.
                TreeNode node = tree.GetNodeAt(pt);
    
                // Add a child node.
                node.Nodes.Add((TreeNode)e.Data.GetData(typeof(TreeNode)));
    
                // Show the newly added node if it is not already visible.
                node.Expand();
            }
    
            private void tvDescriptions_ItemDrag(object sender, ItemDragEventArgs e)
            {
    
            }
    Last edited by PRR; Apr 10 '09, 07:05 AM. Reason: Please post code in [code] [/code] tags.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    So you want a 'move' not a copy? Am I reading that right?
    Do the copy, then at the node where you moved it from perform a .Remove

    At line 6 you learn which node is being copied (naming it 'node')
    between lines 11 and 12 add... tree.Nodes.Remo ve(node);

    Comment

    • Sl1ver
      New Member
      • Mar 2009
      • 196

      #3
      Thanx, you really know your stuff. How or where will i post a event to update the database with the new information?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        where will i post a event to update the database with the new information?
        I don't know your program as well as you do, so I can only give you a generic answer... I would raise and event to perform an update after I'm done with all my changes. Where that fits into what you've engineered, only you know. This treeview could be on its own user control, or it could be on a floating pallet or it could be in the middle of your one and only form. I can't say. But that's the beauty of events... As long as you have another class 'listening' (subscribed) to the event it will still work.

        Comment

        • Sl1ver
          New Member
          • Mar 2009
          • 196

          #5
          Please use
          Code:
           ...your code here
          tags

          Hi,

          this is my coding. I get a error that states "Must use a updatable query" and if you double click on the nodes the whole program freezes. any help?

          Code:
           private void tvLocations_MouseDown(object sender, MouseEventArgs e)
                  {
                      TreeView tree = (TreeView)sender;
          
                      // Get the node underneath the mouse.
                      TreeNode node = tree.GetNodeAt(e.X, e.Y);
          
                      // Start the drag-and-drop operation with a cloned copy of the node.
                      if (node != null)
                      {
                          tree.DoDragDrop(node, DragDropEffects.Copy);
                          //node.Remove();
          
                      }
                  }
          
                  private void tvLocations_DragOver(object sender, DragEventArgs e)
                  {
                      // Get the tree.
                      TreeView tree = (TreeView)sender;
          
          
                      // Drag and drop denied by default.
                      e.Effect = DragDropEffects.None;
          
                      // Is it a valid format?
                      if (e.Data.GetData(typeof(TreeNode)) != null)
                      {
                          // Get the screen point.
                          Point pt = new Point(e.X, e.Y);
          
                          // Convert to a point in the TreeView's coordinate system.
                          pt = tree.PointToClient(pt);
          
                          // Is the mouse over a valid node?
                          TreeNode node = tree.GetNodeAt(pt);
                          if (node != null)
                          {
                              e.Effect = DragDropEffects.Copy;
                              tree.SelectedNode = node;
          
                          }
                      }
                  }
          
                  private void tvLocations_DragDrop(object sender, DragEventArgs e)
                  {
                      try
                      {
                          // Get the tree.
                          TreeView tree = (TreeView)sender;
          
                          tree.BeginUpdate();
          
                          // Get the screen point.
                          Point pt = new Point(e.X, e.Y);
          
                          // Convert to a point in the TreeView's coordinate system.
                          pt = tree.PointToClient(pt);
          
                          // Get the node underneath the mouse.
                          TreeNode node = tree.GetNodeAt(pt);
          
                          TreeNode childNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
          
                          // Remoce this node
                          tree.Nodes.Remove(childNode);
          
                          // Add the node to the new parent
                          node.Nodes.Add(childNode);
          
                          // Show the newly added node if it is not already visible.
                          node.Expand();
          
                          tree.EndUpdate();
          
                          UpdateDBStructure(childNode.Tag, node.Tag);
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show("Error: " + ex.Message);
                      }
                  }
          
                  private void tvLocations_ItemDrag(object sender, ItemDragEventArgs e)
                  {
          
                  }
          
                  private void UpdateDBStructure(object Id, object parentId)
                  {                        
                      OleDbConnection mConn = new OleDbConnection(strCon);
          
                      mConn.Open();            
          
                      string UpdateStr = @"UPDATE qx_AssetLocation SET 
                                           alc__acl_id = " + parentId + 
                                         " where alc__ID = " + Id;
          
                      OleDbCommand myCmd = mConn.CreateCommand();
                      myCmd.CommandText = UpdateStr;
                      try
                      {
                          myCmd.ExecuteNonQuery();
                          //MessageBox.Show("Data added to database");
          
                      }
                      catch (Exception ed)
                      {
                          MessageBox.Show("Error: " + ed.Message, "Error");
                      }
                      finally
                      {
                          myCmd.Dispose();
                          mConn.Close();
                          mConn.Dispose();
                          tvLocations.Nodes.Clear();
                          //PopulateTree();
                      }
          
          
                  }
          Last edited by tlhintoq; Apr 15 '09, 03:24 PM. Reason: Please use [CODE] ...your code here [/CODE] tags

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Put a breakpoint in your UpdateDbStructu re (line 92 or so) and walk through it one step at a time with F-10. See if that helps you find the exact location and nature of the issue.

            Comment

            • Sl1ver
              New Member
              • Mar 2009
              • 196

              #7
              thanx man for the help, things are working

              Comment

              Working...